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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rahul Jain
  • 658
  • 5
  • 20

2 Answers2

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

Al Stead
  • 21
  • 3
  • Probably a very basic proof-of-concept piece of code would make you answer much more helpful. Just a friendly suggestion. – YakovL Jul 15 '16 at 16:30
  • for an implementation of this, see the JavaWorld series starting with [Mapping XML to Java](http://www.javaworld.com/article/2076141/java-se/mapping-xml-to-java--part-1.html) – Nathan Hughes Jul 15 '16 at 16:56
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();
     }

   }
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • 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. – Rahul Jain Feb 08 '13 at 11:07
  • 1
    jaxb is best suited for you than serial parsing – TheWhiteRabbit Feb 08 '13 at 11:09
  • 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'. – Rahul Jain Feb 11 '13 at 12:03
  • thats not related to jaxb, probably something to do with jdbc code – TheWhiteRabbit Feb 11 '13 at 12:26
  • Problem resolved, I have used JAXB to parse this complex data. – Rahul Jain Feb 28 '13 at 12:03