javax.xml.parsers.SAXParser
不支持读取XML注释,会忽略它们。
org.xml.sax.ext.LexicalHandler
能够在使用org.xml.sax.XMLReader解析时捕获XML注释。请参考Stack Overflow帖子或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"
LexicalHandler
不会跟踪元素;您还需要将ContentHandler
设置为解析器,以跟踪其他 XML 内容并能够将注释和元素关联起来。我更新了我的答案,仅打印object
元素的注释。 - Ferdinand Prantl