使用sax解析器问题读取xml文件

时间:2014-08-30 14:27:58

标签: java saxparser

我需要使用sax解析器读取此xml文件,但问题是在不同节点中存在相同的子节点名称。这是文件:

<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 ; 
            }



    }
}

这是我的处理程序类

1 个答案:

答案 0 :(得分:0)

您可以区分位置和&amp; adress通过标记条目&amp;来描述子节点。退出父母。就像您在访问子节点上的文本时,您确切知道您孩子列出的元素。

下面的代码段说明了这一点:

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));
    }
 ...
 }