2017-03-07 6 views
1

SAXパーサーでパス式を指定できますか?私はいくつかの同じ名前タグを持つXMLファイルを持っていますが、それらは異なる要素にあります。それらを区別する方法はありますか?SAXパーサーを使用すると、どのようにXMLファイルをJavaで解析するのですか?

<report id="322077"> 
     <update> 
      <when>1136117311</when> 
      <what>[email protected]</what> 
     </update> 
     <update> 
      <when>1136117422</when> 
      <what>[email protected]</what> 
     </update> 
     </report> 
    <report id="322074"> 
     <update> 
      <when>1136117311</when> 
      <what>[email protected]</what> 
     </update> 
     <update> 
      <when>1136117422</when> 
      <what>[email protected]</what> 
     </update> 
     </report> 
    <report id="322076"> 
     <update> 
      <when>1136117311</when> 
      <what>[email protected]</what> 
     </update> 
     <update> 
      <when>1136117422</when> 
      <what>[email protected]</what> 
     </update> 
     </report> 
+0

http://stackoverflow.com/questions/1863250/is-there-any-xpath-processor-for-sax-model – rpfun12

+0

を参照してください、それはあなたが使用することを必須となります。ここではXMLですSAXパーサ?構造体をナビゲートするのは、まずDOMに読み込む方が簡単です。次に、ノードの検索と処理に利用できるユーティリティー・メソッドがたくさんあります。 – sprinter

+0

SAXパーサーを使用することは必須ではありません。どのように私はDOMでそれを行うことができます – sudo

答えて

0
String xml= // your xml 
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = builderFactory.newDocumentBuilder(); 
Document document = builder.parse(new InputSource(new StringReader(xml))); 
String expression="//update"; // See XPath 
XPathExpression expr = xpath.compile(expression) ; 
    NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET); 
for (int k = 0; k < nodes.getLength(); k++) { 
// Do what you want with that 
関連する問題