-4

i need to read this xml file using sax parser , but the problem is beacuse of the same child node name in different nodes. This is the file:

<MasterData>
  <node1>200000</node1>
  <Location>
    <oa:Code>88</oa:Code>
    <oa:Description languageID="en-us">addres1</oa:Description>
    <oa:Description languageID="ar-ae">addres2</oa:Description>
  </Location>
  <address>
   <oa:Code>55</oa:Code>
   <oa:Description languageID="en-us">Loc1</oa:Description>
   <oa:Description languageID="ar-ae">Loc2</oa:Description>
 </address>
</MasterData>

import java.util.ArrayList; import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;




public class MyHandler2 extends DefaultHandler {


      private MasterData  masterdata= null;
      private IssueAuthority issueAuth = null ; 
      private List<IssueAuthority> issueAuthList  = null ;  



    public MasterData  getMasterData() {
        return masterdata;
    }


    boolean bmaster  = false ; 
    boolean bLiceNum = false;
    boolean bissueAuth = false  ; 
    boolean bautCode = false  ; 
    boolean bautDescAr = false ;
    boolean bautDescEn = false ;


    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {


        if (qName.equalsIgnoreCase("MasterData")) {
            //initialize Employee object and set id attribute
            masterdata = new MasterData();



            System.out.println("MasterData");



    }else if (qName.equalsIgnoreCase("LicenseNumber")) {


            bLiceNum = true;
    System.out.println("qName"+qName);        
    }else if (qName.equalsIgnoreCase("IssueAuthority")) {
                       bissueAuth = true;
    }else if (qName.equalsIgnoreCase("oa:Code")) {


        bautCode = true;
    }
            }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

        if (qName.equalsIgnoreCase("IssueAuthority")) {
            issueAuthList = new ArrayList<IssueAuthority>() ;
            issueAuthList.add(issueAuth) ; 

        }
        if (qName.equalsIgnoreCase("MasterData")) {
            //add Employee object to list
            masterdata.setIssueAuthority(issueAuthList);

        }
    }


    @Override
    public void characters(char ch[], int start, int length) throws SAXException {

        if (bLiceNum) {

            masterdata.setLicenseNumber(new String(ch, start, length));
            bLiceNum = false;
        } else if (bissueAuth) {
            issueAuth   = new  IssueAuthority();
            bissueAuth = false ; 

        }else if(bautCode){

            issueAuth.setCode(Integer.parseInt(new String(ch, start, length)));
            bautCode = false ; 
            }



    }
}

this my handler class

wam
  • 1
  • 1
  • So what is actually our question? Same node names in XML files are common and no problem in general at all. – Thorsten Schöning Aug 30 '14 at 14:40
  • Note that the given XML is not valid: you lack the closing tag, you have different opening tags vs closing tags e.g.: test, ... Running this through a SAX parser will for sure generate SAXExceptions ... – Filip Aug 30 '14 at 14:54
  • acutaully i write the xml file very fast but the real one is in valid format dont worry about this ... the real problem is when iam trying to read node2 for example and do an action ... i am facing problem beacuse it try to read node7 with the same child node name to do the same action ... – wam Aug 30 '14 at 15:51
  • i edit the code to be clear the the problem with the Location node and address the have the same child node name .... – wam Aug 30 '14 at 15:58
  • Please post the code you've written. – Paul Aug 30 '14 at 15:58
  • i post the code @Pual – wam Aug 30 '14 at 16:11
  • I assume you have an issue with reading the different values of the Description child nodes (guessing here as it is not at all clear what the actual issue is for me)? You can differentiate between these child nodes on the same level (adres 1 & 2) by checking their attributes in the startElement method. You can differentiate between the other elements childnodes (location 1 & 2) by checking the entry on the parent : e.g. : test if you have crossed the starting element of address – Filip Aug 30 '14 at 18:52
  • U mean at the end of parent element @Filip – wam Sep 02 '14 at 17:20

1 Answers1

0

You can differentiate between the location & adress Description child nodes by flagging the entry & exit on the parent. Like that you know exactly in which element you child list you are when accessing the text on the child nodes.

The code snippet below illustrates this:

List<String> locationDescriptions = new ArrayList<String>();
List<String> addressDescriptions = new ArrayList<String>();

boolean insideAddress = false  ; 
boolean insideLocation = false ;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
 ...

    if (qName.equalsIgnoreCase("Location")) {
        insideLocation = true;
    }
    if (qName.equalsIgnoreCase("address")) {
        insideAddress = true;
    }

 ...
 }

 @Override
public void endElement(String uri, String localName, String qName) throws SAXException {
...
    if (qName.equalsIgnoreCase("Location")) {
        insideLocation = false;
    }
    if (qName.equalsIgnoreCase("address")) {
        insideAddress = false;
    }

...
}

@Override
public void characters(char ch[], int start, int length) throws SAXException {
..
    if (insideLocation && (bautDescAr || bautDescEn) {
        locationDescriptions.add(new String(ch, start, length));
    }
    if (insideAddress && (bautDescAr || bautDescEn) {
        addressDescriptions.add(new String(ch, start, length));
    }
 ...
 }

Filip
  • 857
  • 8
  • 19