Reputation: 129
I have an xml like this:
<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>
I need to extract inner xml
<Functions xmlns="uri_of_functions_subxml">
<Function1 attr="sth">
<Info>Some_Info</Info>
</Function1>
<Function2>
<Info>Some_Info</Info>
</Function2>
...Functions n...
</Functions>
I have firstly tried getting inner xml with characters method:
public void startElement(String uri, String localName, String tagName, Attributes attributes) throws SAXException {
if (tagName.equalsIgnoreCase("Data")){
buffer = new StringBuffer();}
}
public void characters(char[] ch, int start, int length) throws SAXException {
if (buffer != null) {
buffer.append(new String(ch, start, length).trim());
}
}
public void endElement(String uri, String localName, String tagName) throws SAXException {
if (tagName.equalsIgnoreCase("Data")){
innerXML = buffer.toString().trim();
}
But then i have realized that characters method haven't collected xml properly, it might have refused the special characters like "<", ">".
Link below contains same question but the answer is not applicable for me because the outer xml must be processed as sort of handshake signal, the inner xml must be processed in totally different way.
Java XML parsing: taking inner XML using SAX
Only thing that I need is to collect inner xml properly. But, how to do it? Thanks in advance..
Upvotes: 3
Views: 829
Reputation: 135992
SAX seems to be not the best choice for the job, anyway try
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);
}
output
<?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>
Upvotes: 5