xml模板
<Envelope>
<Header>
<ServiceHeader>
<PubSub_Command>Publish</PubSub_Command>
<PubSub_Topic>/IEM/IEMINFO</PubSub_Topic>
<Requester_ID>IEM</Requester_ID>
<SID>14002601016081614000241000024100</SID>
</ServiceHeader>
</Header>
<Body>
<Root ATTRIBUTE="XML">
<HeaderInfo>
<business_no>IMP160614001</business_no>
<ie_flag>I</ie_flag>
<bonded_type>NB</bonded_type>
<trade_co>2939392823</trade_co>
<trade_name>上海延锋江森座椅有限公司</trade_name>
<contract_no>B681232</contract_no>
<trade_mode>0110</trade_mode>
<cut_mode />
<trans_mode>3</trans_mode>
<pack_no>4</pack_no>
<trade_country>101</trade_country>
<relation_confirm>N</relation_confirm>
<price_confirm>N</price_confirm>
<pay_confirm>N</pay_confirm>
</HeaderInfo>
<ListInfo>
<Item>
<item_index>1</item_index>
<customs_index>1</customs_index>
<g_no />
<hs_code>9401901900</hs_code>
<g_name>9401901900</g_name>
<g_comment>9401901900</g_comment>
<qty>800.0000</qty>
<unit>007</unit>
<qty1>5.8000</qty1>
<unit1>035</unit1>
<qty2 />
<unit2 />
<price>0.58000</price>
<trade_total>464.00</trade_total>
<curr>502</curr>
<org_country>501</org_country>
<to_country>142</to_country>
<duty_mode>1</duty_mode>
<material_no>565 881 559 A等</material_no>
<book_no>SEB0RE4/SEB0RE1</book_no>
</Item>
<Item>
<item_index>2</item_index>
<customs_index>2</customs_index>
<g_no />
<hs_code>9401901900</hs_code>
<g_name>9401901900</g_name>
<g_comment>9401901900</g_comment>
<qty>224.0000</qty>
<unit>007</unit>
<qty1>39.8000</qty1>
<unit1>035</unit1>
<qty2 />
<unit2 />
<price>7.86598</price>
<trade_total>1761.98</trade_total>
<curr>502</curr>
<org_country>502</org_country>
<to_country>142</to_country>
<duty_mode>1</duty_mode>
<material_no>2935369</material_no>
<book_no>SEB0RE9</book_no>
</Item>
</ListInfo>
</Root>
</Body>
</Envelope>
目标,解析并获取root子标签下的标签名与值
后台Main方法test
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.json.JSONObject;
import com.jeecg.decmain.entity.DecMainEntity;
import com.jeecg.decmain.page.DecMainPage;
public class TestXml {
public static void main(String[] args)throws Exception {
JSONObject jsob=new JSONObject();
List<String> reslist = new ArrayList<String>();
SAXReader reader = new SAXReader();
Document document = reader.read(new File("D://test//001xml.xml"));
/**
* 节点对象的操作方法
*/
//获取文档根节点
Element root = document.getRootElement();
//获取根节点下面的所有子节点(不包过子节点的子节点)
List<Element> list = root.elements() ;
//获得指定节点下面的子节点
Element contactElem = root.element("Body").element("Root").element("HeaderInfo");//首先要知道自己要操作的节点。
List<Element> contactList = contactElem.elements();
for (Element e:contactList){
//System.out.println(e.getName()+":"+e.getText());
jsob.put(e.getName(), e.getText());
reslist.add(e.getName()+":'"+e.getText()+"'");
}
Element contactElem2 = root.element("Body").element("Root").element("ListInfo");
//遍历该标签下的所有子标签
for(Iterator emp4=contactElem2.elementIterator();emp4.hasNext();){
Element employee5 = (Element) emp4.next();
/*节点名称和节点内容以KEY VALUE的方式保存到JSON对象*/
for(Iterator emp5=employee5.elementIterator();emp5.hasNext();){
Element employee6 = (Element) emp5.next();
reslist.add(employee6.getName()+":'"+employee6.getText()+"'");
}
}
System.out.println(reslist);
}
}
