Java 使用sax获取内部xml
我有这样一个xml:Java 使用sax获取内部xml,java,xml,xml-parsing,sax,Java,Xml,Xml Parsing,Sax,我有这样一个xml: <Message xmlns="uri_of_message"> <VendorId>1234</VendorId> <SequenceNumber>1</SequenceNumber> ...other important headers... <Data> <Functions xmlns="uri_of_functions_subxml"> <
<Message xmlns="uri_of_message">
<VendorId>1234</VendorId>
<SequenceNumber>1</SequenceNumber>
...other important headers...
<Data>
<Functions xmlns="uri_of_functions_subxml">
<Function1 attr="sth">
<Info>Some_Info</Info>
</Function1>
<Function2>
<Info>Some_Info</Info>
</Function2>
...Functions n...
</Functions>
</Data>
</Message>
但后来我意识到characters方法没有正确收集xml,它可能拒绝了像“”这样的特殊字符
下面的链接包含相同的问题,但答案不适用于我,因为外部xml必须作为某种握手信号处理,而内部xml必须以完全不同的方式处理
我唯一需要的是正确地收集内部xml。但是,怎么做呢?
提前感谢。SAX似乎不是这份工作的最佳选择,无论如何,试试看
SAXParser p = SAXParserFactory.newInstance().newSAXParser();
XMLReader filter = new XMLFilterImpl(p.getXMLReader()) {
private boolean inFunctions;
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (!inFunctions && qName.equals("Functions")) {
inFunctions = true;
}
if (inFunctions) {
super.startElement(uri, localName, qName, atts);
} else {
qName.equals("Functions");
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (inFunctions) {
super.endElement(uri, localName, qName);
if (qName.equals("Functions")) {
inFunctions = false;
}
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (inFunctions) {
super.characters(ch, start, length);
}
}
};
Transformer t = TransformerFactory.newInstance().newTransformer();
Source source = new SAXSource(filter, new InputSource(new FileInputStream("1.xml")));
Result result = new StreamResult(System.out);
t.transform(source, result);
}
输出
<?xml version="1.0" encoding="UTF-8"?><Functions xmlns="uri_of_functions_subxml">
<Function1 attr="sth">
<Info>Some_Info</Info>
</Function1>
<Function2>
<Info>Some_Info</Info>
</Function2>
</Functions>
一些信息
一些信息
您似乎不了解SAX是如何工作的。不能将XML标记视为characters方法的内容。此方法将只拾取XML文档的文本节点(标记之间的内容)。您的问题似乎更像是一项工作,而不是SAX。如果您准备接受一个非SAX的答案,我将向您展示如何做到这一点。我在开始使用SAX时不理解的是,SAX框架在遇到xml文档中不同类型的内容时调用的回调函数。
SAXParser p = SAXParserFactory.newInstance().newSAXParser();
XMLReader filter = new XMLFilterImpl(p.getXMLReader()) {
private boolean inFunctions;
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (!inFunctions && qName.equals("Functions")) {
inFunctions = true;
}
if (inFunctions) {
super.startElement(uri, localName, qName, atts);
} else {
qName.equals("Functions");
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (inFunctions) {
super.endElement(uri, localName, qName);
if (qName.equals("Functions")) {
inFunctions = false;
}
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (inFunctions) {
super.characters(ch, start, length);
}
}
};
Transformer t = TransformerFactory.newInstance().newTransformer();
Source source = new SAXSource(filter, new InputSource(new FileInputStream("1.xml")));
Result result = new StreamResult(System.out);
t.transform(source, result);
}
<?xml version="1.0" encoding="UTF-8"?><Functions xmlns="uri_of_functions_subxml">
<Function1 attr="sth">
<Info>Some_Info</Info>
</Function1>
<Function2>
<Info>Some_Info</Info>
</Function2>
</Functions>