-5

I have the following file in xml and I need to Parse it in Java. The file has more than 200 rows, I just gave two rows as sample.

Please help with best method to parse large files ie DOM, SAX, StaX and a working example to prase this file would be greatly appreciated - Thanks

<?xml version="1.0"?>
<vdb>
    <database_data>
        <table_data name="Messages">
            <row rowid="1">
                <col name="firstname"> yong </col>
                <col name="lastname"> mook </col>
                <col name="nickname"> mkyong </col>
                <col name="salary"> 100000 </col>
             </row>
            <row rowid="2">
                <col name="firstname"> foo </col>
                <col name="lastname"> yah </col>
                <col name="nickname"> fyah </col>
                <col name="salary"> 200000 </col>
            </row>
        </table_data>
    </database_data>
</vdb>
Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
  • 5
    Just wanted to introduce my good friend [*Google*](https://www.google.com/search?q=parse+this+XML+File+in+Java&rlz=1C1CHFX_enUS455US455&sugexp=chrome,mod=0&sourceid=chrome&ie=UTF-8) – Nir Alfasi Sep 01 '12 at 03:07
  • 1
    Hi and welcome to StackOverflow. I don't know if you noticed, but this site is called StackOverflow, not *WriteMyCodeForMeOverflow*. Please try doing a little research and attempt to solve the problem yourself based on what you learn. If you have problems, we'll be happy to help you out. – Roddy of the Frozen Peas Sep 01 '12 at 03:10

1 Answers1

1

Take look at Java API for XML Processing for starters

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • The problem is when I am using SAX parser, It is getting the starting element as col and I want to know in the col element, how do I get the value of name attribute? – user1321074 Sep 01 '12 at 13:51
  • Depending on how your processing the events (in my case DefaultHandler) one of the parameters to the startElement is the Attributes. You need to interrogate this parameter. I'd suggest taking a look at [Attributes.getIndex(String)](http://docs.oracle.com/javase/7/docs/api/org/xml/sax/Attributes.html#getIndex(java.lang.String)) and [Attributes.getValue(int)](http://docs.oracle.com/javase/7/docs/api/org/xml/sax/Attributes.html#getValue(int)) – MadProgrammer Sep 01 '12 at 20:59