3

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..

Community
  • 1
  • 1
user1873190
  • 129
  • 1
  • 2
  • 6
  • 1
    You don't seem to understand how SAX works. You cannot treat XML tags as the content of the characters method. This method will only pick-up ***text nodes*** of an XML document (stuff between tags). Your problem seems more like a job for [StAX](http://en.wikipedia.org/wiki/StAX), than for SAX. If you are ready to accept a non-SAX answer I'll show you how you can do this. – predi Jan 17 '13 at 09:01
  • I thing that I didn't understand when I started with SAX was that the functions where callbacks that the SAX framework calls when it encounters the different types of things in the xml document. – Alexander Kjäll Jan 17 '13 at 09:29

1 Answers1

5

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>

Official Tutorial

Asraful
  • 1,241
  • 18
  • 31
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275