0

I need to read read values from below xml and am using saxparser for this, but am stuck while reading tag it appears multiple times and i am unable to read the next row tag, can any one help me....?

Whole data appear in between tag, and refers different Tables, and tage gives the set on records need to be inserted and refers Column name and value need to be inserted into that particular column .

 <?xml version="1.0" encoding="UTF-8"?>
<DATA>
<TABLEDATA name="web_order_header" rows="1">
<ROW>
<FIELD name="order_id"> 40403141201067683</FIELD>
<FIELD name="order_date"> Mar 14 , 2012</FIELD>
<FIELD name="company_name">Testing</FIELD>
<FIELD name="company_website"> N/A </FIELD>
<FIELD name="customer_firstname">uni</FIELD>
<FIELD name="customer_lastname">u</FIELD>
<FIELD name="email">[email protected]</FIELD>
<FIELD name="billto_contact">uni</FIELD>
<FIELD name="billto_phone">78784</FIELD>
<FIELD name="billto_phone_ext">N/A</FIELD>
<FIELD name="billto_address">ss</FIELD>
<FIELD name="billto_city">mys</FIELD>
<FIELD name="billto_state">Kar</FIELD>
<FIELD name="billto_zip">5678945</FIELD>
<FIELD name="shipto_contact">uni</FIELD>
<FIELD name="shipto_phone">78784</FIELD>
<FIELD name="shipto_phone_ext">N/A</FIELD>
<FIELD name="shipto_address">ss</FIELD>
<FIELD name="shipto_city">mys</FIELD>
<FIELD name="shipto_state">Kar</FIELD>
<FIELD name="shipto_zip">5678945</FIELD>
</ROW>
</TABLEDATA>

<TABLEDATA name="web_order_detail" rows="3">
<ROW>
<FIELD name="order_id"> 40403141201067683</FIELD>
<FIELD name="qty">1</FIELD>
<FIELD name="item_id">JUS72-28250</FIELD>
<FIELD name="mfr">Justrite Manufacturing Co</FIELD>
<FIELD name="item_desc">EcoPolyBlend&amp;trade; Drum Collection Station For Spills</FIELD>
<FIELD name="uom">Each</FIELD>
<FIELD name="price">739.00</FIELD>
</ROW>
<ROW>
<FIELD name="order_id"> 40403141201067683</FIELD>
<FIELD name="qty">1</FIELD>
<FIELD name="item_id">COM62-CAS-B51V80-CA1B</FIELD>
<FIELD name="mfr">Compressed Air Systems</FIELD>
<FIELD name="item_desc">5HP 80 GAL 15CFM Reciprocating Air Compressor</FIELD>
<FIELD name="uom">Each</FIELD>
<FIELD name="price">1265.33</FIELD>
</ROW>
<ROW>
<FIELD name="order_id"> 40403141201067683</FIELD>
<FIELD name="qty">2</FIELD>
<FIELD name="item_id">KIM11-05701</FIELD>
<FIELD name="mfr">Kimberly Clark Corp</FIELD>
<FIELD name="item_desc">Wypall&amp;reg; 12.5&amp;quot; x 13&amp;quot; L40 Q-Fold White Towel</FIELD>
<FIELD name="uom">Case</FIELD>
<FIELD name="price">88.00</FIELD>
</ROW>
</TABLEDATA>
</DATA>

This is my JAVA code

public void readXml(Document requestDoc,PrintStream out)throws IOException, JDOMException, SQLException, ParseException {
             Document responseDoc = null;


             Element root = requestDoc.getRootElement();

             List tableNameList = root.getChildren("TABLEDATA");

             for(int i=0; i<tableNameList.size(); i++){
                 Element table = (Element) tableNameList.get(i);
                 String tableName = table.getAttribute("name").getValue();
                 int numOfRows = table.getAttribute("rows").getIntValue();
                 System.out.println(tableName+" : tableName---------------------- numOfRows : "+numOfRows);

                    List rowList = root.getChild("TABLEDATA").getChildren("ROW");
                    for(int j=0; j<rowList.size(); j++){
                        Element row = (Element) rowList.get(j);
                        System.out.println("row : "+rowList.size());

                        List fieldList =  root.getChild("TABLEDATA").getChild("ROW").getChildren("FIELD");
                        System.out.println("----------------------");
                        for(int k=0; k<fieldList.size(); k++){
                            Element field = (Element) fieldList.get(k);
                            String fieldName = field.getAttribute("name").getValue();
                            String fieldValue = field.getTextTrim();
                            System.out.println(fieldName+"----------------------"+fieldValue);
                        }
                        System.out.println("----------------------");
                    }


             }
1
  • The subject of your Question is misleading as you aren't using SAX at all to parse this XML, you're using DOM. The Document object is a DOM class. Commented Mar 14, 2012 at 16:27

2 Answers 2

2

Your always looking at the first table and the first row. To fix it, use the current elements in the for loops.

Replace

List rowList = root.getChild("TABLEDATA").getChildren("ROW");

with

List rowList = table.getChildren("ROW");

and

List fieldList =  root.getChild("TABLEDATA").getChild("ROW").getChildren("FIELD");

with

List fieldList =  row.getChildren("FIELD");
Sign up to request clarification or add additional context in comments.

Comments

1

You can use XPath (XML Path Language) instead of using DOM, http://www.vogella.com/articles/JavaXML/article.html

Comments

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.