JAXB介绍二
链接上一遍 JAXB介绍一 , 本节主要介绍解析xml的步骤, 下面的例子是在实际项目中运用的, 把它拿出来单独写一个java运行程序.
5. 测试实例
先给出我的代码结构图:
再给出要解析的Script.xml文件的内容:
<?xml version='1.0' encoding='UTF-8' ?> <Script> <SchemaVersion MajorVerNo="1" MinorVerNo="0" /> <Activity> <Action>ReinstallTV</Action> </Activity> <Activity> <Action>TuneChannel</Action> <Priority>1</Priority> <Attributes> <ChannelNumber>340</ChannelNumber> </Attributes> </Activity> </Script>
下面进入正题:
1. 给出Script.java的代码, 其实就是对Script.xml的类似Bean构建:
package com.demo.util; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "Script") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder = {"schemaVersion","activities"}) public class Script { @XmlElement(name = "SchemaVersion", required = false) private SchemaVersion schemaVersion; @XmlElement(name = "Activity", required = false) private List<Activity> activities; public SchemaVersion getSchemaVersion() { return schemaVersion; } public void setSchemaVersion(SchemaVersion schemaVersion) { this.schemaVersion = schemaVersion; } public List<Activity> getActivity() { return activities; } public void setActivity(List<Activity> activities) { this.activities = activities; } }
2. 给出SchemaVersion.java的代码:
package com.demo.util; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "SchemaVersion") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder = {"majorVerNo","minorVerNo"}) public class SchemaVersion { @XmlAttribute(name = "MajorVerNo", required = false) protected String majorVerNo; @XmlAttribute(name = "MinorVerNo", required = false) protected String minorVerNo; public String getMajorVerNo() { return majorVerNo; } public void setMajorVerNo(String majorVerNo) { this.majorVerNo = majorVerNo; } public String getMinorVerNo() { return minorVerNo; } public void setMinorVerNo(String minorVerNo) { this.minorVerNo = minorVerNo; } }
3. 给出Activity.java的代码结构:
package com.demo.util; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "Activity") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder = {"action","priority","attributes"}) public class Activity { @XmlElement(name = "Action", required = false) protected String action; @XmlElement(name = "Priority", required = false) protected String priority; @XmlElement(name = "Attributes", required = false) protected Attributes attributes; public String getAction() { return action; } public void setAction(String action) { this.action = action; } public String getPriority() { return priority; } public void setPriority(String priority) { this.priority = priority; } public Attributes getAttributes() { return attributes; } public void setAttributes(Attributes attributes) { this.attributes = attributes; } }
4. 给出Attributes.java的代码:
package com.demo.util; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "Attributes") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder = {"channelNumber"}) public class Attributes { @XmlElement(name = "ChannelNumber", required = false) protected String channelNumber; public String getChannelNumber() { return channelNumber; } public void setChannelNumber(String channelNumber) { this.channelNumber = channelNumber; } }
5. 给出Test.java对Script.xml处理的代码:
package com.demo.test; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import com.demo.util.Activity; import com.demo.util.Script; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub
// Script.xml路径可以随时改变 String SCRIPT_XML_PATH = "C:/Script/Script.xml"; try { File file = new File(SCRIPT_XML_PATH); JAXBContext jaxbContext = JAXBContext.newInstance(Script.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Script script = (Script) unmarshaller.unmarshal(file); System.out.println("SchemaVersion=>MajorVerNo="+script.getSchemaVersion().getMajorVerNo()); System.out.println("SchemaVersion=>MinorVerNo="+script.getSchemaVersion().getMinorVerNo()); boolean hasReinstallTvCommand = false; if (script != null) { List<Activity> activities = script.getActivity(); int activitySize = activities.size(); if (activitySize > 0) { for(Activity activity : activities) { System.out.println("Activity:"); if (activity.getAction() != null) { System.out.println(" Action="+activity.getAction()); if ("ReinstallTV".equalsIgnoreCase(activity.getAction())) { hasReinstallTvCommand = true; //break; } } if (activity.getPriority() != null) { System.out.println(" Priority="+activity.getPriority()); } if (activity.getAttributes() != null) { if (activity.getAttributes().getChannelNumber() != null) { System.out.println(" ChannelNumber="+activity.getAttributes().getChannelNumber()); } } } } } System.out.println("hasReinstallTvCommand="+hasReinstallTvCommand); } catch (JAXBException e) { System.out.println("e="+e.getMessage()); }
6. 给出最后Test.java运行的代码, 打印出的log:
SchemaVersion=>MajorVerNo=1
SchemaVersion=>MinorVerNo=0
Activity:
Action=ReinstallTV
Activity:
Action=TuneChannel
Priority=1
ChannelNumber=340
hasReinstallTvCommand=true
待续...
努力做一个伪程序员, 不管道路有多么的艰苦......