1

I need to parse a nested XML using SAX Parser.

<ParentNode  id="3" name="3" >
    <ChildNode>
        <InnerNode id="13" name="13"   parentID="3" />
    </ChildNode>
    <ChildNode>
        <ParentNode id="2" name="2" >
            <ChildNode>
                <ParentNode id="1" name="1" >
                    <ChildNode>
                        <InnerNode id="11" name="11"   parentID="1" />
                    </ChildNode>
                    <ChildNode>
                        <InnerNode id="10" name="10"   parentID="1" />
                    </ChildNode>
                </ParentNode>
            </ChildNode>
            <ChildNode>
                <InnerNode id="12" name="12"   parentID="2" />
            </ChildNode>
        </ParentNode>
    </ChildNode>
</ParentNode>

ParentNode tag is the main tag here. ParentNode can also be inside the ChildNode tag. Here onyToMany relationship between ParentNode and ChildNode. and oneToOne relationship between ChildNode and InnerNode in hibernate domains.

This is not static XML. It can be more nested.

1

2 Answers 2

2

A design pattern to consider if you are wanting to use the SAX parser is defining a stack of the objects (e.g. Stack objectStack = new Stack()). Push the corresponding object onto the stack in startElement. Pop the object off the stack in endElement. Peek at the top of the stack when you want to know the containing (parent) object. (e.g. in startElement, if you peek at the top of the stack and there is an object there, it is the parent of the object defined by the current element.)

Sign up to request clarification or add additional context in comments.

2 Comments

Probably a very basic proof-of-concept piece of code would make you answer much more helpful. Just a friendly suggestion.
for an implementation of this, see the JavaWorld series starting with Mapping XML to Java
0

This should help

public static void main(String argv[]) {

    try {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    DefaultHandler handler = new DefaultHandler() {

    boolean bfname = false;
    boolean blname = false;
    boolean bnname = false;
    boolean bsalary = false;

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

        System.out.println("Start Element :" + qName);



    }

    public void endElement(String uri, String localName,
        String qName) throws SAXException {

        System.out.println("End Element :" + qName);

    }

 saxParser.parse("yourfile.xml", handler);

     } catch (Exception e) {
       e.printStackTrace();
     }

   }

5 Comments

I am already using SAXParser using above technique but I need to save objects within objects. means ParentNode class objects should conatins the list of ChildNode and ChildNode object should contains the InnerNode object. At last when outer ParentNode object will be ready that time it will be saved in the database containing all information about inner objects.
jaxb is best suited for you than serial parsing
Now I am using JAXB and getting object of ParentNode that conatins all information about its child but i am now not able to save this object via hibernate. it's giving error :- com.microsoft.sqlserver.jdbc.SQLServerException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FKAF2F9C9B7BD425D8". The conflict occurred in database "chart", table "dbo.ParentNode", column 'id'.
thats not related to jaxb, probably something to do with jdbc code
Problem resolved, I have used JAXB to parse this complex data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.