我認爲你可以做到這一點的方式..它比起上述
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
是簡單
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();
}
}
}
解編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();
}
}
}
你應該使用這樣@XmlRootElement(名稱=「行動 - 根「)公共課ActionRoot – Naren
okk,但我可以創建2類,一個行動和其他對於rep-params,因爲我創建了一個只顯示一個而不是其他的一個xml文件,即 oadd_location_id –
monu
你想忽略輸出中的總rep-params標籤? – Naren