java返回xml并解析_Java获取并解析XML(JavaBean和XML
的转化)
最近遇到⼀个问题,需要从⼀个url获取xml⽂档并解析,google了很多⽅法,⼤多数都
是获取到XML然后循环遍历拿到数据,但是感觉很⿇烦,并且感觉当数据量⽐较⼤的时候效率也不怎么
⾼,后⾯研究了下,发现JAXB这个好东西,可以实现JavaBean和XML节点元素的互相转换,故此总结⼀
下:
1、相关链接
2、准备⼯作
需要安装JAXB和dom4j,如果⽤的Maven,可以访问上⾯的url,并搜索JAXB和dom4j,选择需要的版本
,复制下⾯的代码配置到⾃⼰的项⽬中:
3、获取并解析
先看⼀下返回的XML格式
e="北京市">
解析
public static void main(String[] args) {
//获取XML
SAXReader reader = new SAXReader();
org.dom4j.Document document = ad(new URL("api.************************ */v2.0/hotel/l")));
String documentStr = document.asXML();
//将xml字符串转换为java对象
JaxbUtil resultBinder = new JaxbUtil(HotelGeos.class, JaxbUtil.CollectionWrapper.class);
HotelGeos hotelObj = resultBinder.fromXml(documentStr);
//将java对象转换为XML字符串
HotelGeos hotelGeos=new HotelGeos();
List hotelGeoList=new ArrayList<>();
HotelGeo hotelGeo =new HotelGeo();
hotelGeo.setCityCode("0101");
hotelGeo.setCityName("北京市");
hotelGeo.setCountry("中国");
hotelGeo.setProvinceId("0100");
hotelGeo.setProvinceName("北京市");
//其他层级的这⾥就不⼀⼀赋值了
hotelGeoList.add(hotelGeo);
hotelGeos.setHotelGeoList(hotelGeoList);
JaxbUtil requestBinder = new JaxbUtil(HotelGeos.class, CollectionWrapper.class); String retXml = Xml(hotelGeos, "utf-8");
System.out.println("xml:"+retXml);
}
相关类之HotelGeos
package com .*****.geoData.items;
import lombok.Data;
使用dom4j解析xml文件l.bind.annotation .*;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Steven
* Date: 2019/12/05
* Time: 13:36
*/
@Data
@XmlRootElement(name = "HotelGeos")
public class HotelGeos {
@XmlElementWrapper(name = "HotelGeoList")
@XmlElement(name = "HotelGeo")
private List HotelGeoList;
}
HotelGeo
package com.***.geoData.items;
import lombok.Data;
dehaus.jackson.map.annotate.JsonSerialize;
l.bind.annotation.XmlAttribute;
l.bind.annotation.XmlElement;
l.bind.annotation.XmlElementWrapper;
import java.io.Serializable;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Steven
* Date: 2019/12/06
* Time: 13:38
*/
@Data
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class HotelGeo implements Serializable {
@XmlAttribute(name = "CityCode")
private String CityCode;
@XmlAttribute(name = "CityName")
private String CityName;
@XmlAttribute(name = "Country")
private String Country;
@XmlAttribute(name = "ProvinceId")
private String ProvinceId;
@XmlAttribute(name = "ProvinceName")
private String ProvinceName;
@XmlElementWrapper(name = "CommericalLocations")
@XmlElement(name = "Location") //这⾥需要格外注意,层级很容易写错private List CommericalLocations;
@XmlElementWrapper(name = "Districts")
@XmlElement(name = "Location")
private List Districts;
@XmlElementWrapper(name = "LandmarkLocations")
@XmlElement(name = "Location")
private List LandmarkLocations;
}
Location
package com.*****.geoData.items;
import lombok.Data;
dehaus.jackson.map.annotate.JsonSerialize;
l.bind.annotation.XmlAttribute;
import java.io.Serializable;
/**
* Created by IntelliJ IDEA.
* User: Steven
* Date: 2019/12/06
* Time: 13:53
*/
@Data
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class Location implements Serializable {
@XmlAttribute(name = "Id")
private String Id;
@XmlAttribute(name = "Name")
private String Name;
}
JaxbUtil⼯具类
package com.*****.utils;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
l.bind.JAXBContext;
l.bind.JAXBElement;
l.bind.JAXBException;
l.bind.Marshaller;
l.bind.Unmarshaller;
l.bind.annotation.XmlAnyElement;
l.namespace.QName;
import org.apachemons.lang.StringUtils;
/**
* 使⽤Jaxb2.0实现XMLJava Object的Binder.
*
* 特别⽀持Root对象是List的情形.
*
* @author
*/public class JaxbUtil {
// 多线程安全的Context.
private JAXBContext jaxbContext;
/**
* @param types
* 所有需要序列化的Root对象的类型.
*/
public JaxbUtil(Class>... types) {
try {
jaxbContext = wInstance(types);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* Java Object->Xml.
*/
public String toXml(Object root, String encoding) {
try {
StringWriter writer = new StringWriter();
createMarshaller(encoding).marshal(root, writer);
String();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* Java Object->Xml, 特别⽀持对Root Element是Collection的情形.
*/
@SuppressWarnings("unchecked")
public String toXml(Collection root, String rootName, String encoding) { try {
CollectionWrapper wrapper = new CollectionWrapper();
JAXBElement wrapperElement = new JAXBElement
per>(
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论