2011-06-29 93 views
2

因此,我目前使用SAX嘗試從我正在處理的許多xml文檔中提取一些信息。到目前爲止,提取屬性值非常容易。但是,我不知道如何去從文本節點中提取實際值。從XML文件中提取文本節點在JAVA中使用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" /> 

我可以提取由正從VAL值「突出顯示」沒有問題。但我不知道如何進入該文本節點,並出去「文本提取」。

這裏是我的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。這樣,characters方法就知道它在適當的元素內,並且它將讀取的文本存儲在StringBuilder中。 endElement方法在到達元素的末尾時調用。因此,您可以獲取StringBuilder的內容並將其存儲在任何你想要的地方。我只將它存儲在一個局部變量(theCompleteText)中,但如果需要,可以將它存儲在一個實例變量中。 –

+0

你可以去除那個布爾值,並在字符方法中測試'if(textBuilder!= null)'。 – megaflop

相關問題