从XML文件中提取文本节点在JAVA中使用SAX解析器

问题描述:

因此,我目前使用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这是一个容易得多... –

下面是一些伪代码:

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