XML Parsing - DOM or SAX - Complex xml with attributes as conditions to access hierarchy in java

    <playingTestCodeDetails classCode="ENT" determinerCode="INSTANCE" >
                  <realmCode code="QD" />
                  <id assigningAuthorityName="PRMORDCODE" extension="16494"  />
                  <id assigningAuthorityName="TESTNUMINBOOK" extension="16494"  />
                  <code code="16494" codeSystemName="QTIM" displayName="SureSwab Candidiasis" /> 
                  <name  use=""></name>
                  <asSeeAlsoCode classCode="ROL" > <!-- Have repeated Seealsocode section for multiple see also codes and stripped names -->
                    <realmCode code="QD" />
                    <code code="7600" displayName="Sample See Also Name" ></code>
                  <asSeeAlsoCode classCode="ROL" >
                    <realmCode code="QD" />
                    <code code="6496" displayName="Sample See Also Name" ></code>
<subjectOf  typeCode="SBJ">
              <realmCode code="QD" />
              <order classCode="OBS" moodCode="EVN" >
                <realmCode code="QD" />                              
                <performer nullFlavor="" typeCode="PRF"><!-- Have added this to accomodate the UnitCode-->
                  <performingLocatedEntity classCode="LOCE" nullFlavor="">
                    <locatedPerformingSite classCode="ORG" determinerCode="INSTANCE">
                      <id assigningAuthorityName="ASORDERED" extension="16494" />
                <origin nullFlavor="" typeCode="ORG"> <!-- Have added this to accomodate the Ordering Lab Code-->
                    <orderingLocatedEntity classCode="LOCE" >
                       <locatedOrderingSite classCode="ORG" determinerCode="INSTANCE">
                        <id assigningAuthorityName="PRMORDCODE"  extension="16494"/>
                        <code code="SJC" codeSystemName="QTIM" codeSystem="ORDERINGLABCODE"/>
                <pertinentInformation1 typeCode="PERT">
                    <realmCode code="QD" /> 
                    <clinicalInfo classCode="ACT" moodCode="EVN">
                      <realmCode code="QD" /> 
                <subjectOf  typeCode="SUBJ">
                  <realmCode code="QD" />
                  <annotation classCode="ACT" moodCode="EVN" >
                    <realmCode code="QD" />
                    <code code="DOSCATNAME"></code>
                    <text><![CDATA[SureSwab<sup>&reg;</sup>, <em>Candidiasis</em>, PCR]]></text>

I have a xml looking like above. I want to parse it; what is the best way to parse it?? DOM, SAX ( i have heard of JAXB, XSLT,.... not sure of this two); Can we have a combination of DOM & SAX to parse a XML??

A simple scenario to attain a tag value using attribute access as "code" like when code=DOSCATNAME in tag then we need to take up data for corresponding tag.

Other scenario is to access tag and get the hierarchy and access extension attribute of when assigningAuthorityName attribute has value PRMORDCODE.

Can the above two scenarios can be achievable using a Parser??

I am a newbie, please understand what i need to parse & suggest me a thought... thanks in advance...


Use JAXB. Create class model and annotate your classes appropriately. The environment will do the rest.

For example you should create class PlayingTestCodeDetails with properties classCode, determinerCode etc.

I will tell you more: you can kindly ask JAXB to generate the classes for you. Start learning from this article: http://www.roseindia.net/jaxb/r/jaxb.shtml

It will take a couple of hours to start but than you will be done in 15 minutes. If you are using DOM you can start in 15 minutes of learning and the coding a couple of days to parse your XML.

It depends on your need which to use.

Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation.

SAX • Parses node by node • Doesn’t store the XML in memory • We cant insert or delete a node • SAX is an event based parser • SAX is a Simple API for XML • doesn’t preserve comments • SAX generally runs a little faster than DOM

DOM • Stores the entire XML document into memory before processing • Occupies more memory • We can insert or delete nodes • Traverse in any direction. • DOM is a tree model parser • Document Object Model (DOM) API • Preserves comments • SAX generally runs a little faster than DOM

If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.

These are few parsers:-

  1. woodstox
  2. dom4j

In addition to SAX and DOM there is STaX parsing available using XMLStreamReader which is an xml pull parser.

Need Your Help

How do I unroll (compile) an interpreter loop?

c optimization compiler-construction programming-languages interpreter

I heard that some languages go from interpreted to compiled by "unrolling the interpreter loop".

How to pass value to a function in javascript (googleMap)

javascript google-maps google-maps-api-3

I am following https://developers.google.com/maps/documentation/javascript/directions

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.