Pregunta

We are performing some operations on embedded/Nested XML.I am using SAXParser to parse the entire XML file.I want to get the entire nested XML with tags and value.For example my XML looks like.

I want entire XML within the <ANY_ELEMENT>.....</ANY-ELEMENT> tag.

<?xml version="1.0" encoding="UTF-8"?>
            <x:xMessage xmlns:x="http://www.connecture.com/integration/x" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                            xsi:schemaLocation="http://www.connecture.com/integration/x xMessageWrapper.xsd
                ">
                <x:xMessageHeader>
                    <Version>850</Version>
                    <Source>Source</Source>
                    <Target>target</Target>
                    <Timestamp>2013-12-31T12:00:00</Timestamp>
                    <RequestID>123456</RequestID>
                    <ResponseID>54321</ResponseID>
                    <Priority>3</Priority>
                    <Username>Deepak</Username>
                    <Password>Kumar</Password>
                </x:xMessageHeader>
                <x:xMessageBody>
                    <ANY-ELEMENT>
                        <xEnveloped_834A1 xsi:schemaLocation="....." xmlns="......."
                            ..........................
                    ..........................
                            some Complex XML
                        ..........................
                        ..........................
                        ..........................

                    </ANY-ELEMENT>

                 </x:XMessageBody>
        </x:XMessage>

Handler class Sample code:

public class MessageWrapperHandler extends DefaultHandler {


    private boolean bActualMessage = false;
    private String actualMessage = null;
    private long lengthActualMessage=0;



    public void startElement(String uri, String localName, String qName, Attributes attributes) {

      if (qName.equalsIgnoreCase("ANY-ELEMENT")) {
            bActualMessage = true;
            //lengthActualMessage=How to know the length of Child XML
        }
    }
  public void characters(char ch[], int start, int length) {

         if (bActualMessage) {
            actualMessage = new String(ch, start, length);
            //trying to get embedded XML
            bActualMessage = false;
        }
    }

}

But since next element after is XML content so giving me nothing.SO How to achieve it. EDIT: You are free to modify XML after <ANY-ELEMENT> like adding contents into CDATA

¿Fue útil?

Solución

Instead of SAX, I would recommend using StAX (a StAX implementation is included in the JDK/JRE since Java SE 6). StAX is similar to SAX except instead of having the events pushed to you, you pull (request) them.

In the code below the XMLStreamReader is advanced to the ANY-ELEMENT element. Once it is at the correct position you can interact with it as you wish.

import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();

        StreamSource xmlSource = new StreamSource("src/forum19559825/input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(xmlSource);

        Demo demo = new Demo();
        demo.positionXMLStreamReaderAtAnyElement(xsr);
        demo.processAnyElement(xsr);
    }

    private void positionXMLStreamReaderAtAnyElement(XMLStreamReader xsr) throws Exception {
        while(xsr.hasNext()) {
            if(xsr.getEventType() == XMLStreamReader.START_ELEMENT && "ANY-ELEMENT".equals(xsr.getLocalName())) {
                break;
            }
            xsr.next();
        }
    }

    private void processAnyElement(XMLStreamReader xmlStreamReaderAtAnyElement) {
        // TODO: Stuff
        System.out.println("FOUND IT");
    }

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top