当存在重复标记时,在JAVA中解析XML

当存在重复标记时,在JAVA中解析XML,java,xml-parsing,Java,Xml Parsing,我需要从下面的xml中读取值,并且正在使用saxparser进行此操作,但是在读取标记时被卡住了它多次出现,并且我无法读取下一行标记,有人能帮我吗 整个数据显示在标记之间,并引用不同的表,tage给出需要插入的记录集,并引用需要插入到该特定列中的列名和值 <?xml version="1.0" encoding="UTF-8"?> <DATA> <TABLEDATA name="web_order_header" rows="1"> <ROW>

我需要从下面的xml中读取值,并且正在使用saxparser进行此操作,但是在读取标记时被卡住了它多次出现,并且我无法读取下一行标记,有人能帮我吗

整个数据显示在标记之间,并引用不同的表,tage给出需要插入的记录集,并引用需要插入到该特定列中的列名和值

 <?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">aaa@xyz.com</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>

40403141201067683
2012年3月14日
测试
不适用
统一
U
aaa@xyz.com
统一
78784
不适用
党卫军
迈斯
卡尔
5678945
统一
78784
不适用
党卫军
迈斯
卡尔
5678945
40403141201067683
1.
JUS72-28250
佳思特制造公司
EcoPolyBlend&;贸易;溢出物桶收集站
每个
739
40403141201067683
1.
COM62-CAS-B51V80-CA1B
压缩空气系统
5HP 80 GAL 15CFM往复式空气压缩机
每个
1265.33
40403141201067683
2.
KIM11-05701
金伯利克拉克公司
威宝律师事务所;规则;12.5及;引用;x13&;引用;L40 Q折白色毛巾
案例
88
这是我的JAVA代码

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("----------------------");
                    }


             }
public void readXml(Document requestDoc,PrintStream out)抛出IOException、jdomeexception、SQLException、ParseException{
文档响应DC=null;
Element root=requestDoc.getRootElement();
List tableNameList=root.getChildren(“TABLEDATA”);

for(int i=0;i您总是查看第一个表和第一行。要修复它,请使用for循环中的当前元素

替换

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


您总是查看第一个表和第一行。若要修复它,请使用for循环中的当前元素

替换

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

您可以使用XPath(XML路径语言)代替DOM,

您可以使用XPath(XML路径语言)代替DOM,

问题的主题具有误导性,因为您根本没有使用SAX来解析此XML,而是在使用DOM。
文档
对象是一个DOM类。
问题的主题具有误导性,因为您根本没有使用SAX来解析此XML,而是在使用DOM。
文档
对象是一个DOM类。
List fieldList =  root.getChild("TABLEDATA").getChild("ROW").getChildren("FIELD");
List fieldList =  row.getChildren("FIELD");