<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<action-root>
<action name="EMPLOYERS">
<name>EMPLOYERS</name>
<version>12</version>
<param>5</param>
</action>
</action-root>
heey我想在解组时打印下面的xml文件数据,以便我应该做哪些更改
答案 0 :(得分:0)
我认为你可以这样做..与上面相比很简单
ActionRoot.java
package com.naren;
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;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"action"
})
@XmlRootElement(name = "action-root")
public class ActionRoot {
@XmlElement(required = true)
private Action action;
/**
* Gets the value of the action property.
*
* @return
* possible object is
* {@link ActionRoot.Action }
*
*/
public Action getAction() {
return action;
}
/**
* Sets the value of the action property.
*
* @param value
* allowed object is
* {@link ActionRoot.Action }
*
*/
public void setAction(Action value) {
this.action = value;
}
}
Action.java
package com.naren;
import java.util.List;
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.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "reportName", "reportVersion",
"repNoOfParam", "repParams" })
public class Action {
@XmlElement(name = "report-name", required = true)
private String reportName;
@XmlElement(name = "report-version")
private int reportVersion;
@XmlElement(name = "rep-no-of-param")
private int repNoOfParam;
@XmlElement(name = "rep-params")
private List<RepParams> repParams;
@XmlAttribute
private String name;
public String getReportName() {
return reportName;
}
public void setReportName(String reportName) {
this.reportName = reportName;
}
public int getReportVersion() {
return reportVersion;
}
public void setReportVersion(int reportVersion) {
this.reportVersion = reportVersion;
}
public int getRepNoOfParam() {
return repNoOfParam;
}
public void setRepNoOfParam(int repNoOfParam) {
this.repNoOfParam = repNoOfParam;
}
public List<RepParams> getRepParams() {
return repParams;
}
public void setRepParams(List<RepParams> repParams) {
this.repParams = repParams;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
RepParams.java
package com.naren;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "value" })
public class RepParams {
@XmlAttribute
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@XmlAttribute
private int id;
@XmlValue
private String value;
}
用于封送TESTXMLGEN.java
package com.naren;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class TestXMLGen {
/**
* @param args
*/
public static void main(String[] args) {
ActionRoot actionRoot = new ActionRoot();
Action action = new Action();
action.setName("EMPLOYERS");
action.setRepNoOfParam((byte) 5);
action.setReportVersion((byte) 12);
action.setReportName("EMPLOYERS");
List<RepParams> list = new ArrayList<RepParams>();
RepParams repParams = new RepParams();
repParams.setId((byte) 0);
repParams.setName("P_LOCATION_ID");
repParams.setValue("oadd_location_id");
RepParams repParams2 = new RepParams();
repParams2.setId((byte) 1);
repParams2.setName("P_FINYEAR_ID");
repParams2.setValue("oadd_fin_year_id");
list.add(repParams);
list.add(repParams2);
action.setRepParams(list);
actionRoot.setAction(action);
try {
File file = new File("file.xml");
JAXBContext jaxbContext1 = JAXBContext
.newInstance(ActionRoot.class);
Marshaller jaxbMarshaller = jaxbContext1.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(actionRoot, file);
jaxbMarshaller.marshal(actionRoot, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Unmarshalling TestUnMarshalXML.java
package com.naren;
import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class TestUnMarshalXML {
public static void main(String[] args) {
try {
File file = new File("file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(ActionRoot.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ActionRoot aroot = (ActionRoot) jaxbUnmarshaller.unmarshal(file);
Action action = aroot.getAction();
List<RepParams> repParams=action.getRepParams();
System.out.println(action.getName()+"\t"+action.getReportName()+"\t"+action.getReportVersion());
for(RepParams rp:repParams)
System.out.println(rp.getId()+"\t"+rp.getName()+"\t"+rp.getValue());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}