2011-06-29 6 views
2

私は現在、私が取り組んでいるいくつかのXML文書からいくつかの情報を抽出しようとしています。これまでのところ、属性値を抽出するのは簡単です。しかし、私はテキストノードから実際の値を抽出する方法を知りません。JAXでSAXパーサーを使用してXMLファイルからテキストノードを抽出する

<w:rStyle w:val="Highlight" /> 
    </w:rPr> 
    </w:pPr> 
- <w:r> 
    <w:t>Text to Extract</w:t> 
    </w:r> 
    </w:p> 
- <w:p w:rsidR="00B41602" w:rsidRDefault="00B41602" w:rsidP="007C3A42"> 
- <w:pPr> 
    <w:pStyle w:val="Copy" /> 

私はヴァルから値を取得することにより、「ハイライト」は問題を抽出することはできません:指定されたXML文書に例えば

、。しかし、私はどのようにテキストノードに入り、 "Text to Extract"を取得するのか分かりません。ここで

は私のJavaコードは

private static final class SaxHandler extends DefaultHandler 
    { 
     // invoked when document-parsing is started: 
     public void startDocument() throws SAXException 
     { 
      System.out.println("Document processing starting:"); 
     } 

     // notifies about finish of parsing: 
     public void endDocument() throws SAXException 
     { 
      System.out.println("Document processing finished. \n"); 
     } 

     // we enter to element 'qName': 
     public void startElement(String uri, String localName, 
       String qName, Attributes attrs) throws SAXException 
     { 
      if(qName.equalsIgnoreCase("Relationships")) 
      { 
       // do nothing 
      } 
      else if(qName.equalsIgnoreCase("Relationship")) 
      { 
       // goes into the element and if the attribute is equal to "Target"... 
       String val = attrs.getValue("Target"); 
       // ...and the value is not null 
       if(val != null) 
       { 
        // ...and if the value contains "image" in it... 
        if (val.contains("image")) 
        { 
         // ...then get the id value 
         String id = attrs.getValue("Id"); 
         // ...and use the substring method to isolate and print out only the image & number 
         int begIndex = val.lastIndexOf("/"); 
         int endIndex = val.lastIndexOf("."); 
         System.out.println("Id: " + id + " & Target: " + val.substring(begIndex+1, endIndex)); 
        } 
       } 
      } 
      else 
      { 
       throw new IllegalArgumentException("Element '" + 
         qName + "' is not allowed here"); 
      } 
     } 

     // we leave element 'qName' without any actions: 
     public void endElement(String uri, String localName, String qName) throws SAXException 
     { 
      // do nothing; 
     } 
    } 

...属性値を引き出すために、これまであるしかし、私はそのテキストノードに入るために開始し、内部の値を引き出す見当もつかない。誰でもいくつかのアイデアがありますか?ここで

+0

は、あなたはそれが非常に簡単であるXPathを使用して考えがあります... –

答えて

5

は、いくつかの擬似コードです:

private boolean insideElementContainingTextNode; 
private StringBuilder textBuilder; 

public void startElement(String uri, String localName, String qName, Attributes attrs) { 
    if ("w:t".equals(qName)) { // or is it localName? 
     insideElementContainingTextNode = true; 
     textBuilder = new StringBuilder(); 
    } 
} 

public void characters(char[] ch, int start, int length) { 
    if (insideElementContainingTextNode) { 
     textBuilder.append(ch, start, length); 
    } 
} 

public void endElement(String uri, String localName, String qName) { 
    if ("w:t".equals(qName)) { // or is it localName? 
     insideElementContainingTextNode = false; 
     String theCompleteText = this.textBuilder.toString(); 
     this.textBuilder = null; 
    } 
} 
+0

うーん、私はそれを試してみましたが、それは任意のテキストを抽出しませんでした。そのコードが何をすべきか説明できますか? –

+0

startElementでは、抽出するテキストノードを含む要素の読み込みをパーザが開始するかどうかをチェックします。はいの場合、ブール変数をtrueに設定します。この方法では、文字メソッドは適切な要素の内部にあることを認識し、読み取られたテキストをStringBuilderに格納します。メソッドのendElementは、要素の終わりに達すると呼び出されます。したがって、StringBuilderの内容を取得し、必要に応じて保存することができます。私はローカル変数(theCompleteText)にのみ格納しましたが、必要に応じてインスタンス変数に格納することができます。 –

+0

代わりに、そのブール値を取り除き、代わりにcharactersメソッドで 'if(textBuilder!= null)'をテストすることができます。 – megaflop

関連する問題