簡體   English   中英

在 JAVA 中使用 SAX 解析器從 XML 文件中提取文本節點

[英]Extracting Text Nodes From XML File Using SAX Parser in JAVA

所以我目前正在使用 SAX 嘗試從我正在使用的一些 xml 文檔中提取一些信息。 到目前為止,提取屬性值真的很容易。 但是,我不知道如何從文本節點中提取實際值。

例如,在給定的 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;
        }
     }

但我不知道從哪里開始進入該文本節點並提取其中的值。 有人有什么想法嗎?

這是一些偽代碼:

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM