java解析XML
来源:互联网 发布:马耳他十字机芯的优化 编辑:程序博客网 时间:2024/06/09 14:24
废话不多说。
一个简单的解析xml,希望对某些朋友有所借鉴。
新建立一个web的工程,测试使用。
把这些jar包放入lib下
测试解析的xml如下:
<?xml version="1.0" encoding="utf-8"?><glass version="123"><work><job><id>job0001</id><workId>work0001</workId><name>GS01</name><type>abedo1</type><tag>mod09GA0</tag><rule><start>2000-1-1</start><timeUnit>year</timeUnit><timeLength>1</timeLength><stop>2005-12-32</stop></rule><rule><start>2006-1-1</start><timeUnit>month</timeUnit><timeLength>6</timeLength><stop>2009-12-31</stop></rule></job><job><id>job0002</id><workId>work0002</workId><name>GS02</name><type>abedo2</type><tag>mod09GA1</tag><rule><start>2000-1-1</start><timeUnit>year</timeUnit><timeLength>2</timeLength><stop>2005-12-32</stop></rule><rule><start>2006-1-1</start><timeUnit>month</timeUnit><timeLength>7</timeLength><stop>2009-12-31</stop></rule></job></work></glass>
建立一个解析需要的xsd
同时注意将namespace相关清除(如果规范由我们定 可以有namespace 但是这个生成的xml与规范中是不同的会加带namespace)
建立需要的Typs
xsd代码如下:
<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:complexType name="glass"><xsd:sequence><xsd:element name="work" type="work"></xsd:element></xsd:sequence><xsd:attribute name="version" type="xsd:string"></xsd:attribute></xsd:complexType><xsd:complexType name="work"><xsd:sequence><xsd:element name="job" type="job"></xsd:element> </xsd:sequence></xsd:complexType><xsd:complexType name="job"><xsd:sequence><xsd:element name="id" type="xsd:string"></xsd:element><xsd:element name="workId" type="xsd:string"></xsd:element><xsd:element name="name" type="xsd:string"></xsd:element><xsd:element name="type" type="xsd:string"></xsd:element><xsd:element name="tag" type="xsd:string"></xsd:element><xsd:element name="rule" type="rule"></xsd:element></xsd:sequence></xsd:complexType> <xsd:complexType name="rule"> <xsd:sequence> <xsd:element name="start" type="xsd:string"></xsd:element> <xsd:element name="timeUnit" type="xsd:string"></xsd:element> <xsd:element name="timeLength" type="xsd:string"></xsd:element> <xsd:element name="stop" type="xsd:string"></xsd:element> </xsd:sequence> </xsd:complexType> <xsd:element name="glass" type="glass"></xsd:element></xsd:schema>
把xsd拷贝到c:下一份
写好xsd文件后用xjc工具来帮助我们生成XSD对应的java映射类、点击运行选择当前的工程 同时注意那两个勾勾上然后点击serach
后输入xjc会看见XJCFacade 点击ok7
Ok 解析类生成成功!!!
主要生成的代码可能少根节点标记如果少了主要添加:
Glass::
//// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.6 生成的// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // 在重新编译源模式时, 对此文件的所有修改都将丢失。// 生成时间: 2014.03.07 时间 04:41:12 PM CST //package com.neusoft.test.jaxd;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlType;/** * <p>glass complex type的 Java 类。 * * <p>以下模式片段指定包含在此类中的预期内容。 * * <pre> * <complexType name="glass"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="work" type="{}work"/> * </sequence> * <attribute name="version" type="{http://www.w3.org/2001/XMLSchema}string" /> * </restriction> * </complexContent> * </complexType> * </pre> * * */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "glass", propOrder = { "work"})@XmlRootElement(name="glass")public class Glass { @XmlElement(required = true) protected Work work; @XmlAttribute(name = "version") protected String version; /** * 获取work属性的值。 * * @return * possible object is * {@link Work } * */ public Work getWork() { return work; } /** * 设置work属性的值。 * * @param value * allowed object is * {@link Work } * */ public void setWork(Work value) { this.work = value; } /** * 获取version属性的值。 * * @return * possible object is * {@link String } * */ public String getVersion() { return version; } /** * 设置version属性的值。 * * @param value * allowed object is * {@link String } * */ public void setVersion(String value) { this.version = value; }}
Job
//// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.6 生成的// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // 在重新编译源模式时, 对此文件的所有修改都将丢失。// 生成时间: 2014.03.07 时间 04:41:12 PM CST //package com.neusoft.test.jaxd;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlType;/** * <p>job complex type的 Java 类。 * * <p>以下模式片段指定包含在此类中的预期内容。 * * <pre> * <complexType name="job"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="id" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="workId" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="type" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="tag" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="rule" type="{}rule"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "job", propOrder = { "id", "workId", "name", "type", "tag", "rule"})public class Job { @XmlElement(required = true) protected String id; @XmlElement(required = true) protected String workId; @XmlElement(required = true) protected String name; @XmlElement(required = true) protected String type; @XmlElement(required = true) protected String tag; @XmlElement(required = true) protected Rule rule; /** * 获取id属性的值。 * * @return * possible object is * {@link String } * */ public String getId() { return id; } /** * 设置id属性的值。 * * @param value * allowed object is * {@link String } * */ public void setId(String value) { this.id = value; } /** * 获取workId属性的值。 * * @return * possible object is * {@link String } * */ public String getWorkId() { return workId; } /** * 设置workId属性的值。 * * @param value * allowed object is * {@link String } * */ public void setWorkId(String value) { this.workId = value; } /** * 获取name属性的值。 * * @return * possible object is * {@link String } * */ public String getName() { return name; } /** * 设置name属性的值。 * * @param value * allowed object is * {@link String } * */ public void setName(String value) { this.name = value; } /** * 获取type属性的值。 * * @return * possible object is * {@link String } * */ public String getType() { return type; } /** * 设置type属性的值。 * * @param value * allowed object is * {@link String } * */ public void setType(String value) { this.type = value; } /** * 获取tag属性的值。 * * @return * possible object is * {@link String } * */ public String getTag() { return tag; } /** * 设置tag属性的值。 * * @param value * allowed object is * {@link String } * */ public void setTag(String value) { this.tag = value; } /** * 获取rule属性的值。 * * @return * possible object is * {@link Rule } * */ public Rule getRule() { return rule; } /** * 设置rule属性的值。 * * @param value * allowed object is * {@link Rule } * */ public void setRule(Rule value) { this.rule = value; }}
ObjectFactory
//// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.6 生成的// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // 在重新编译源模式时, 对此文件的所有修改都将丢失。// 生成时间: 2014.03.07 时间 04:41:12 PM CST //package com.neusoft.test.jaxd;import javax.xml.bind.JAXBElement;import javax.xml.bind.annotation.XmlElementDecl;import javax.xml.bind.annotation.XmlRegistry;import javax.xml.namespace.QName;/** * This object contains factory methods for each * Java content interface and Java element interface * generated in the com.neusoft.test.jaxd package. * <p>An ObjectFactory allows you to programatically * construct new instances of the Java representation * for XML content. The Java representation of XML * content can consist of schema derived interfaces * and classes representing the binding of schema * type definitions, element declarations and model * groups. Factory methods for each of these are * provided in this class. * */@XmlRegistrypublic class ObjectFactory { private final static QName _Glass_QNAME = new QName("", "glass"); /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.neusoft.test.jaxd * */ public ObjectFactory() { } /** * Create an instance of {@link Glass } * */ public Glass createGlass() { return new Glass(); } /** * Create an instance of {@link Work } * */ public Work createWork() { return new Work(); } /** * Create an instance of {@link Rule } * */ public Rule createRule() { return new Rule(); } /** * Create an instance of {@link Job } * */ public Job createJob() { return new Job(); } /** * Create an instance of {@link JAXBElement }{@code <}{@link Glass }{@code >}} * */ @XmlElementDecl(namespace = "", name = "glass") public JAXBElement<Glass> createGlass(Glass value) { return new JAXBElement<Glass>(_Glass_QNAME, Glass.class, null, value); }}
Rule
//// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.6 生成的// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // 在重新编译源模式时, 对此文件的所有修改都将丢失。// 生成时间: 2014.03.07 时间 04:41:12 PM CST //package com.neusoft.test.jaxd;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlType;/** * <p>rule complex type的 Java 类。 * * <p>以下模式片段指定包含在此类中的预期内容。 * * <pre> * <complexType name="rule"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="start" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="timeUnit" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="timeLength" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="stop" type="{http://www.w3.org/2001/XMLSchema}string"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "rule", propOrder = { "start", "timeUnit", "timeLength", "stop"})public class Rule { @XmlElement(required = true) protected String start; @XmlElement(required = true) protected String timeUnit; @XmlElement(required = true) protected String timeLength; @XmlElement(required = true) protected String stop; /** * 获取start属性的值。 * * @return * possible object is * {@link String } * */ public String getStart() { return start; } /** * 设置start属性的值。 * * @param value * allowed object is * {@link String } * */ public void setStart(String value) { this.start = value; } /** * 获取timeUnit属性的值。 * * @return * possible object is * {@link String } * */ public String getTimeUnit() { return timeUnit; } /** * 设置timeUnit属性的值。 * * @param value * allowed object is * {@link String } * */ public void setTimeUnit(String value) { this.timeUnit = value; } /** * 获取timeLength属性的值。 * * @return * possible object is * {@link String } * */ public String getTimeLength() { return timeLength; } /** * 设置timeLength属性的值。 * * @param value * allowed object is * {@link String } * */ public void setTimeLength(String value) { this.timeLength = value; } /** * 获取stop属性的值。 * * @return * possible object is * {@link String } * */ public String getStop() { return stop; } /** * 设置stop属性的值。 * * @param value * allowed object is * {@link String } * */ public void setStop(String value) { this.stop = value; }}
Work
//// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.6 生成的// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // 在重新编译源模式时, 对此文件的所有修改都将丢失。// 生成时间: 2014.03.07 时间 04:41:12 PM CST //package com.neusoft.test.jaxd;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlType;/** * <p>work complex type的 Java 类。 * * <p>以下模式片段指定包含在此类中的预期内容。 * * <pre> * <complexType name="work"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="job" type="{}job"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "work", propOrder = { "job"})public class Work { @XmlElement(required = true) protected Job job; /** * 获取job属性的值。 * * @return * possible object is * {@link Job } * */ public Job getJob() { return job; } /** * 设置job属性的值。 * * @param value * allowed object is * {@link Job } * */ public void setJob(Job value) { this.job = value; }}
下面编写测试代码:
MyTest
package com.neusoft.test.myTest;import java.io.StringReader;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Unmarshaller;import com.neusoft.test.jaxd.Glass;public class MyTest{ /** * @param args */ public static void main(String[] args) { //TODO 需要写出方法的具体实现 String myXml="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+ "<glass version=\"123\">" + "<work>" + "<job><id>job0001</id><workId>work0001</workId><name>GS01</name><type>abedo</type>" + "<tag>mod09GA</tag>" + "<rule><start>2000-1-1</start><timeUnit>year</timeUnit><timeLength>1</timeLength><stop>2005-12-32</stop></rule>" + "<rule>" + "<start>2006-1-1</start>" + "<timeUnit>month</timeUnit>" + "<timeLength>6</timeLength>" + "<stop>2009-12-31</stop></rule>" + "</job>" + "<job><id>job0002</id><workId>work0002</workId><name>GS02</name><type>abedo</type>" + "<tag>mod09GA</tag>" + "<rule><start>2000-1-1</start><timeUnit>year</timeUnit><timeLength>1</timeLength><stop>2005-12-32</stop>" + "</rule>" + "<rule>" + "<start>2006-1-1</start><timeUnit>month</timeUnit><timeLength>6</timeLength>" + "<stop>2009-12-31</stop>" + "</rule></job></work></glass>"; //TODO 需要写出方法的具体实现 JAXBContext jaxbContent; Glass glass ; try { jaxbContent = JAXBContext.newInstance(Glass.class); Unmarshaller m = jaxbContent.createUnmarshaller(); glass = (Glass)m.unmarshal(new StringReader(myXml)); System.out.println(myXml); System.out.println(glass.getVersion()); System.out.println(glass.getWork().getJob().getId()); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Ok 成功解析
0 0
- [xml]java解析xml ,怀旧
- java解析xml 克隆xml
- Java解析XML (DOM解析XML)
- xml解析---Java解析xml文件
- java 解析 xml文件
- java 解析xml
- JAVA解析XML DOM
- java如何解析xml
- JAVA解析XML DOM
- java 解析XML
- JAVA解析xml
- java解析xml字符串
- Java解析XML文档
- JAVA 解析XML文档
- JAVA解析XML DOM
- java解析Xml
- java jdom 解析xml
- java解析XML
- uva:10085 - The most distant state
- Linux Play Log
- JFinal 体验心得
- OS X H3C L2TP VPN
- 手机软件测试方法
- java解析XML
- 一台电脑哦上配置tomcat集群
- 《JAVA与模式》之享元模式
- 快速判断你的电脑是否适合安装win8系统
- NGUI研究院之自制 Scroll View实现触摸滚动相册效果(四)
- 简单描述下linux下如何扩充,删除,管理LVM
- Dungeon Master -uva
- 废旧木质音箱/收录机的重生
- 网络映射命令