3

我想在解析这个 XML 内容时识别标签。我们有 isStartElement() 或 isEndElement() 之类的方法。但这不是属于这两个类别。请帮助我。如何解决这种标签。

代码 :

 import java.io.StringReader;
 import java.util.Hashtable;
 import java.util.Iterator;

 import javax.xml.stream.Location;
 import javax.xml.stream.XMLEventReader;
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.events.EndElement;
 import javax.xml.stream.events.StartElement;
 import javax.xml.stream.events.XMLEvent;

 public class Sample {

public static void main(String[] args) {
    String xPath = "HAI/ONE";       
    String xml = "<HAI><ONE/></HAI>";
    try {
        System.out.println(getValueByXPath(xml,xPath));

    } catch (Exception e) {
        e.printStackTrace();
    }




}     
 public  String getValueByXPath(String xmlString, String xPathString){
    String value = "";
    String tagName = null;
    int index = 2;
    int hashIndex = 1;
    int tagBalance = 0;
    String tagIndex = null;
    String[] tagIndexArr = null;
    Hashtable<Integer,String> xPathHashtable = null;

    XMLInputFactory xmlInputFactory;
    XMLEventReader xmlEventReader;
    try{            
        xmlInputFactory = XMLInputFactory.newInstance();
        xmlInputFactory.setProperty  
          (XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,Boolean.TRUE);       
        xmlInputFactory.setProperty
          (XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,Boolean.FALSE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, 
           Boolean.FALSE);   
        xmlEventReader = xmlInputFactory.createXMLEventReader(new StringReader
           (xmlString));

        //Split XPath 
        xPathHashtable = splitXPath(xPathString);

        tagIndex = xPathHashtable.get(hashIndex);
        tagIndexArr = tagIndex.split("=");
        tagName = tagIndexArr[0];
        index = Integer.parseInt(tagIndexArr[1])+1;

        while (xmlEventReader.hasNext()) { 
            XMLEvent e = xmlEventReader.nextEvent(); 

            if(tagBalance < 0 ){
                return "";
            }

            if(index==1 && e.isCharacters()){
                value = e.asCharacters().getData();
                hashIndex++;
                tagIndex = xPathHashtable.get(hashIndex);
                if(tagIndex == null)break;
                tagIndexArr = tagIndex.split("=");
                tagName = tagIndexArr[0];
                index = Integer.parseInt(tagIndexArr[1])+1;
                tagBalance = 0;
                System.out.println(tagName+"tag"+index);

            }
            if (e.isStartElement()) {                   
                StartElement startElement = e.asStartElement(); 
                if(tagName.equalsIgnoreCase(startElement.getName
                                                  ().getLocalPart())){
                    index--;
                    tagBalance++;
                }
            }   

            if (e.isEndElement()) { 

                EndElement endElement = e.asEndElement();

                if(tagName.equalsIgnoreCase(endElement.getName
                                                 ().getLocalPart())){                       
                    tagBalance--;
                                        }
            }
        }

    }catch(Exception e){
        e.printStackTrace();
    }
    return value;
}

public  Hashtable<Integer,String> splitXPath(String xPath){

    Hashtable<Integer,String> xPathHashtable = null;
    String[] xPathSplit = null;
    int hashIndex = 0;

    int index = 1;
    String tag= null;
    try{
        xPathHashtable = new Hashtable<Integer,String>();
        xPathSplit = xPath.split("[\\[\\]/]");

        for(int i=0;i<xPathSplit.length;i++){

            if(xPathSplit[i].isEmpty()) continue;

            try{
                index = Integer.parseInt(xPathSplit[i]);
            }catch(Exception e){
                tag = xPathSplit[i];
                index = 1;
                hashIndex++;
            }
            xPathHashtable.put(hashIndex, tag+"="+index);               
        }

    }catch(Exception e){
        e.printStackTrace();
    }

    return xPathHashtable;
}
 }

我正在使用 Stax 解析器模拟 XPath 场景。

4

1 回答 1

0

我和你有同样的问题,在任何地方都找不到解决方案。

你想要做的是使用

xmlEventReader.peek().isEndElement()  

并随心所欲地处理。peek()不会像那样移动元素.nextEvent()

于 2017-05-24T20:02:21.157 回答