java中 使用jdom 解析和生成循环嵌套的xml格式文件
程序员文章站
2022-05-28 12:36:05
...
xml 和bean的相互转换。在公司里很多接口的实现都是通过请求xml参数,得到xml后进行解析然后获取需要的信息,最后还要返回xml格式的参数来进行验证。因此这里介绍一种开源项目JDOM来解析和生成xml,它基于树型结构,利用纯JAVA的技术对XML文档实现解析、生成、序列化以及多种操作。JDOM 直接为JAVA编程服务。它利用更为强有力的JAVA语言的诸多特性(方法重载、集合概念以及映射),把SAX和DOM的功能有效地结合起来。
准备工作:下面是要解析的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Xml>
<ReportType>HZD</ReportType>
<Title>业务</Title>
<Body>
<DateTime>2016-09-13</DateTime>
<Name>ekihheiiu</Name>
<Number>H8349567</Number>
<OrderItems>
<OrderItem>
<TypeName>套餐</TypeName>
<PhoneNo Value ="KDYX5)">
<Item>
<Name>单一宽</Name>
<BeginTime>安装完成当天</BeginTime>
<EndTime>2017年09月</EndTime>
</Item>
</PhoneNo>
</OrderItem>
<OrderItem>
<TypeName>可选包</TypeName>
<PhoneNo Value=" 138788">
<Item>
<Name>中国人</Name>
<BeginTime>2017年10月01日</BeginTime>
<EndTime>--</EndTime>
</Item>
</PhoneNo>
</OrderItem>
</OrderItems>
<Remark>测试</Remark>
</Body>
</Xml>
需要使用到的工具类 XMLUtil ,这个类封装了JDOM的具体实现,提供了常用的一些方法,避免直接使用JDOM的API。
1.解析xml文件(将xml转换成bean)
实现思路 : ① 实例化XMLUtil的工具类。
② 通过实例化的工具类和XPath来得到xml文件各节点下的数据
③ 用bean来保存xml中的数据 以供使用
实现代码
package test.xml;
public class HZDXml {
private String reportType;
private String title;
private HZDBody hzdBody;
public String getReportType() {
return reportType;
}
public void setReportType(String reportType) {
this.reportType = reportType;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public HZDBody getHzdBody() {
return hzdBody;
}
public void setHzdBody(HZDBody hzdBody) {
this.hzdBody = hzdBody;
}
}
package test.xml;
import java.util.List;
public class HZDBody {
private String dateTime;
private String name;
private String number;
private String Remark;
private List<HZDOrderItem> HZDOrderItem;
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getRemark() {
return Remark;
}
public void setRemark(String remark) {
Remark = remark;
}
public List<HZDOrderItem> getHZDOrderItem() {
return HZDOrderItem;
}
public void setHZDOrderItem(List<HZDOrderItem> hZDOrderItem) {
HZDOrderItem = hZDOrderItem;
}
}
package test.xml;
import java.util.List;
import test.OrderItemList;
public class HZDOrderItem {
private String typeName;
private List<PhoneNo> phoneNo;
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public List<PhoneNo> getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(List<PhoneNo> phoneNo) {
this.phoneNo = phoneNo;
}
}
package test.xml;
public class PhoneNo {
private String value;
private String name;
private String beginTime;
private String endTime;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBeginTime() {
return beginTime;
}
public void setBeginTime(String beginTime) {
this.beginTime = beginTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public static HZDXml getHZDXMLDataByStr() throws Exception {
String xmlContent =XMLUtil.xml2Str("resources/template/HZD.xml"); // 在项目中读取本地的一个xml文件
HZDXml HzdXml=null;
XMLUtil xMLUtil = XMLUtil.getInsance(new ByteArrayInputStream(xmlContent
.getBytes("UTF-8"))); //初始化XMLUtil工具类
HzdXml =new HZDXml();
HzdXml.setReportType(xMLUtil.getSingleElementText("Xml/ReportType")); //得到Xml文件中 Xml节点下的ReportType
// System.out.println("ReportType: "+HzdXml.getReportType());
HzdXml.setTitle(xMLUtil.getSingleElementText("Xml/Title"));
HZDBody hzdBodyah=new HZDBody();
hzdBodyah.setDateTime(xMLUtil.getSingleElementText("Xml/Body/DateTime"));
hzdBodyah.setName(xMLUtil.getSingleElementText("Xml/Body/Name"));
// System.out.println("Name: "+hzdBodyah.getName());
hzdBodyah.setNumber(xMLUtil.getSingleElementText("Xml/Body/Number"));
// System.out.println("Number: "+hzdBodyah.getNumber());
hzdBodyah.setRemark(xMLUtil.getSingleElementText("Xml/Body/Remark"));
// System.out.println("Remark: "+hzdBodyah.getRemark());
List<Element>HzdOrderItemLists=xMLUtil.getAllElements("Xml/Body/OrderItems/OrderItem");
//得到xml数据中 OrderItems节点下的所有OrderItem节点的集合
int i=0;
List<HZDOrderItem> HzdOrderItemList=new ArrayList<HZDOrderItem>();
for(Element element:HzdOrderItemLists){
//对获得到的OrderItem的集合进行遍历
HZDOrderItem HzdOrderItem=new HZDOrderItem();
HzdOrderItem.setTypeName(element.getChildTextTrim("TypeName"));
// System.out.println("TypeName :"+HzdOrderItem.getTypeName());
//取得PhoneNo节点下的所有Item节点的集合
List<Element>PhoneNoList = element.getChildren("PhoneNo");
String value=PhoneNoList.get(i).getAttributeValue("Value");
List<Element> ItemList = element.getChild("PhoneNo").getChildren("Item");
List<PhoneNo>phoneNoList=new ArrayList();
for(Element element2 : ItemList){ //对Item集合进行遍历
PhoneNo phoneNo = new PhoneNo();
phoneNo.setName(element2.getChildTextTrim("Name"));
System.out.println("Name :"+phoneNo.getName());
phoneNo.setBeginTime(element2.getChildTextTrim("BeginTime"));
phoneNo.setEndTime(element2.getChildTextTrim("EndTime"));
phoneNo.setValue(value);
System.out.println("Value :"+phoneNo.getValue());
phoneNoList.add(phoneNo);
}
HzdOrderItem.setPhoneNo(phoneNoList);
HzdOrderItemList.add(HzdOrderItem);
}
hzdBodyah.setHZDOrderItem(HzdOrderItemList);
HzdXml.setHzdBody(hzdBodyah);
return HzdXml;
}
2.生成的xml文件 (将bean对象转换生成xml文件)
<XMLData>
<AreaCode>551</AreaCode>
<Source>1001</Source>
<Persons>
<person>
<PersonType>1</PersonType>
<CardID>083356</CardID>
</person>
</Persons>
<Ext1>扩充</Ext1>
<Ext2>扩充</Ext2>
<Ext3>扩充</Ext3>
</XMLData>
public String createQueryPrePhotoXML(QueryPrePhotoXML queryPhotoXML) {
String xml = "";
Element XMLData = new Element("XMLData"); //创建根节点
Element AreaCode = new Element("AreaCode");
AreaCode.setText(queryPhotoXML.getAreaCode());
Element Source = new Element("Source");
Source.setText(queryPhotoXML.getSource());
Element Ext1 = new Element("Ext1");
Ext1.setText(queryPhotoXML.getExt1());
Element Ext2 = new Element("Ext2");
Ext2.setText(queryPhotoXML.getExt2());
Element Ext3 = new Element("Ext3");
Ext3.setText(queryPhotoXML.getExt3());
Element Persons=new Element("Persons");
Element person=new Element("person");
Element PersonType=new Element("PersonType");
PersonType.setText(queryPhotoXML.getPerson().getPersonType());
Element CardID=new Element("CardID");
CardID.setText(queryPhotoXML.getPerson().getCardID());
// 表头
XMLData.addContent(AreaCode);
XMLData.addContent(Source);
person.addContent(PersonType);
person.addContent(CardID);
Persons.addContent(person);
XMLData.addContent(Persons);
XMLData.addContent(Ext1);
XMLData.addContent(Ext2);
XMLData.addContent(Ext3);
Document doc = new Document(XMLData);
XMLOutputter out = new XMLOutputter();
Format format = Format.getCompactFormat();
format.setEncoding("UTF-8");
format.setIndent("\t");
out.setFormat(format);// 设置文件编码,默认为UTF-8
ByteArrayOutputStream bo = new ByteArrayOutputStream();
try {
out.output(doc, bo);
logger.info("QueryPrePhoto:"
+ new String(bo.toByteArray(), "UTF-8"));
xml = new String(bo.toByteArray(), "UTF-8");
} catch (Exception e) {
logger.error(e.getMessage());
}
return xml;
}
public class QueryPrePhotoXML {
private String AreaCode;
private String Source;
private String Ext1;
private String Ext2;
private String Ext3;
private Persons person;
public String getAreaCode() {
return AreaCode;
}
public void setAreaCode(String areaCode) {
AreaCode = areaCode;
}
public String getSource() {
return Source;
}
public void setSource(String source) {
Source = source;
}
public String getExt1() {
return Ext1;
}
public void setExt1(String ext1) {
Ext1 = ext1;
}
public String getExt2() {
return Ext2;
}
public void setExt2(String ext2) {
Ext2 = ext2;
}
public String getExt3() {
return Ext3;
}
public void setExt3(String ext3) {
Ext3 = ext3;
}
public Persons getPerson() {
return person;
}
public void setPerson(Persons person) {
this.person = person;
}
}
package com.goldgrid.post.bean;
public class Persons {
private String PersonType;
private String CardID;
public String getPersonType() {
return PersonType;
}
public void setPersonType(String personType) {
PersonType = personType;
}
public String getCardID() {
return CardID;
}
public void setCardID(String cardID) {
CardID = cardID;
}
}
上一篇: JAVA使用JDOM读取XML文件