2017-01-08 76 views
1

我想在Java中使用SAX解析器在我的XML文件中僅讀取對象標記的註釋。如何使用SAX Java解析器閱讀評論文本

這是我的文件的摘要:

<!-- Object Seed term: day, WikiTitle: day--> 
<object id="15155220" name="solar day, twenty-four hour period, 24-hour interval, mean solar day, twenty-four hours, si day, día, days, si days, day duration, day, civil day"> 
    <!-- class: "calendar day" --> 
    <class id="15157041" name="calendar day, civil day"></class> 
    <!-- class: "unit of time" --> 
    <class id="15154774" name="time units, unit of time, time unit, units of time"></class> 
    <!-- class: "" --> 
    <class id="15113229" name="period of time, time period, period"></class> 
    <!-- class: "" --> 
    <class id="00000000" name="time"></class> 
    <genericPhysicalDescription> 
     <!-- hasPart: "" --> 
     <hasPart id="15228378" name="hour, time of day"></hasPart> 
     <!-- hasPart: "" --> 
     <hasPart id="15157225" name="day"></hasPart> 
     <!-- partOf: "calendar" --> 
     <partOf id="15173479" name="calendrics, calendar, dating style, calendarist, calendars, birthday calendar, calendar strip, secular calendar, calandar, agriculture calendar, calendar system, criminal calendar"></partOf> 
     <!-- partOf: "" --> 
     <partOf id="15206296" name="month"></partOf> 
     <!-- partOf: "" --> 
     <partOf id="15157225" name="day"></partOf> 
    </genericPhysicalDescription> 
</object> 
+0

是的,我的應用程序是基於SAX解析器 – Fast

+0

僅供參考,薩克斯有些解析XML .. –

回答

0

javax.xml.parsers.SAXParser不支持讀取評論。它忽略了它們。

org.xml.sax.ext.LexicalHandler允許您在與org.xml.sax.XMLReader解析時捕獲註釋。請參閱another stackoverflow posttutorial at Oracle上的示例。

如果要將註釋連接到緊跟其後的元素,您還可以將org.xml.sax.ContentHandler傳遞給解析器,並通過它跟蹤其他XML內容。我適應上面所指打印只object元素,即將由註釋前面的代碼:

import org.xml.sax.*; 
import org.xml.sax.ext.*; 
import org.xml.sax.helpers.*; 

import java.io.IOException; 

public class Test implements LexicalHandler, ContentHandler { 

    private String lastComment; 

    public void startDTD(String name, String publicId, String systemId) throws SAXException { 
    } 
    public void endDTD() throws SAXException { 
    } 
    public void startEntity(String name) throws SAXException { 
    } 
    public void endEntity(String name) throws SAXException { 
    } 
    public void startCDATA() throws SAXException { 
    } 
    public void endCDATA() throws SAXException { 
    } 
    public void comment(char[] text, int start, int length) throws SAXException { 
    this.lastComment = new String(text, start, length).trim(); 
    } 

    public void characters(char[] ch, int start, int length) { 
    } 
    public void endDocument() { 
    } 
    public void endElement(String uri, String localName, String qName) { 
    } 
    public void endPrefixMapping(String prefix) { 
    } 
    public void ignorableWhitespace(char[] ch, int start, int length) { 
    } 
    public void processingInstruction(String target, String data) { 
    } 
    public void setDocumentLocator(Locator locator) { 
    } 
    public void skippedEntity(String name) { 
    } 
    public void startDocument() { 
    } 
    public void startElement(String uri, String localName, String qName, Attributes atts) { 
    if (localName == "object") { 
     if (this.lastComment != null) { 
     System.out.println("Element object with comment found: \"" + this.lastComment + "\""); 
     this.lastComment = null; 
     } 
    } else { 
     this.lastComment = null; 
    } 
    } 
    public void startPrefixMapping(String prefix, String uri) { 
    } 

    public static void main(String[] args) { 
    Test test = new Test(); 
    XMLReader parser; 

    try { 
     parser = XMLReaderFactory.createXMLReader(); 
    } catch (SAXException ex1) { 
     try { 
     parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); 
     } catch (SAXException ex2) { 
     return; 
     } 
    } 

    try { 
     parser.setProperty("http://xml.org/sax/properties/lexical-handler", test); 
    } catch (SAXNotRecognizedException e) { 
     System.out.println(e.getMessage()); 
     return; 
    } catch (SAXNotSupportedException e) { 
     System.out.println(e.getMessage()); 
     return; 
    } 

    parser.setContentHandler(test); 

    try { 
     parser.parse("test.xml"); 
    } catch (SAXParseException e) { 
     System.out.println(e.getMessage()); 
    } catch (SAXException e) { 
     System.out.println(e.getMessage()); 
    } catch (IOException e) { 
     System.out.println(e.getMessage()); 
    } 
    } 
} 

這段代碼保存爲「Test.java」和你的XML內容「的test.xml」。一旦編譯和執行,它應該給你以下的輸出:

$ javac Test.java 
$ java Test 
Element object with comment found: "Object Seed term: day, WikiTitle: day" 
+0

這個代碼開始讀時的過時的和過時的方法在解析之後的所有評論e – Fast

+0

確實,'LexicalHandler'不跟蹤元素;您還需要將「ContentHandler」設置爲解析器,以跟蹤其他XML內容並能夠將註釋和元素關聯在一起。我更新了我的答案,只打印了'object'元素的註釋。 –

+0

謝謝,它的工作! – Fast