I have an XML element with nodes of the same name that I need to unmarshal. I need service in a List or Array. Here is my XML:
我有一个XML元素,其中包含我需要解组的同名节点。我需要List或Array中的服务。这是我的XML:
<provider name="foo">
<service active="true" name="alias" timeout="N/A">value1</service>
<service active="true" name="caption" timeout="N/A">value2</service>
<service active="true" name="expect_manifest_file" timeout="15m">value3</service>
<service active="true" name="expect_SD_and_HD_ADI" timeout="15m">value4</service>
</provider>
I only know how to get the last element in the XML. How do I make a list of all <service>
nodes and can the list include the active
and name
attribute? Here is my object:
我只知道如何获取XML中的最后一个元素。如何列出所有
@XmlRootElement (name="provider")
public class Customer {
String service;
public String getService() {
return service;
}
@XmlElement(name="service")
public void setService(String service) {
this.service = service;
}
}
1 个解决方案
#1
I'd make another approach according to your needs, maybe this is what you want, let me know.
我会根据你的需要采取另一种方法,也许这就是你想要的,让我知道。
First I will create Service class which will handle the information of your Service node:
首先,我将创建Service类,它将处理您的Service节点的信息:
@XmlRootElement(name = "service")
@XmlAccessorType (XmlAccessType.FIELD)
public class Service {
private boolean active;
private String name;
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Then I will create the class "Customer" which is needed to hold the services you are including in your XML:
然后我将创建“Customer”类,这是保存XML中包含的服务所需的:
@XmlRootElement(name = "provider")
@XmlAccessorType (XmlAccessType.FIELD)
public class Customer {
@XmlElementWrapper
@XmlElement(name = "service")
private List<Service> services = null;
public List<Service> getServices() {
return services;
}
public void setServices(List<Service> services) {
this.services = services;
}
}
I prepared some methods in a main class to make sure this is working:
我在主类中准备了一些方法以确保它正常工作:
public class JaxbHandler {
public void marshal(String path, Customer customer) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, System.out);
jaxbMarshaller.marshal(customer, new File(path));
}
public Customer unmarshal(String path) throws JAXBException {
Customer cust = null;
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
cust = (Customer) jaxbUnmarshaller.unmarshal(new File(path));
return cust;
}
public static void main(String[] args) throws JAXBException {
String path = "c:/dev/sample.xml";
JaxbHandler sampleHandler = new JaxbHandler();
List<Service> myServices = new ArrayList<Service>();
Service s1 = new Service();
s1.setActive(true);
s1.setName("Service A");
Service s2 = new Service();
s2.setActive(true);
s2.setName("Service B");
myServices.add(s1);
myServices.add(s2);
Customer cust = new Customer();
cust.setServices(myServices);
sampleHandler.marshal(path, cust);
Customer backAgain = sampleHandler.unmarshal(path);
}
}
Please let me know if this works for you. Happy coding :)
如果这对你有用,请告诉我。快乐编码:)
#1
I'd make another approach according to your needs, maybe this is what you want, let me know.
我会根据你的需要采取另一种方法,也许这就是你想要的,让我知道。
First I will create Service class which will handle the information of your Service node:
首先,我将创建Service类,它将处理您的Service节点的信息:
@XmlRootElement(name = "service")
@XmlAccessorType (XmlAccessType.FIELD)
public class Service {
private boolean active;
private String name;
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Then I will create the class "Customer" which is needed to hold the services you are including in your XML:
然后我将创建“Customer”类,这是保存XML中包含的服务所需的:
@XmlRootElement(name = "provider")
@XmlAccessorType (XmlAccessType.FIELD)
public class Customer {
@XmlElementWrapper
@XmlElement(name = "service")
private List<Service> services = null;
public List<Service> getServices() {
return services;
}
public void setServices(List<Service> services) {
this.services = services;
}
}
I prepared some methods in a main class to make sure this is working:
我在主类中准备了一些方法以确保它正常工作:
public class JaxbHandler {
public void marshal(String path, Customer customer) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, System.out);
jaxbMarshaller.marshal(customer, new File(path));
}
public Customer unmarshal(String path) throws JAXBException {
Customer cust = null;
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
cust = (Customer) jaxbUnmarshaller.unmarshal(new File(path));
return cust;
}
public static void main(String[] args) throws JAXBException {
String path = "c:/dev/sample.xml";
JaxbHandler sampleHandler = new JaxbHandler();
List<Service> myServices = new ArrayList<Service>();
Service s1 = new Service();
s1.setActive(true);
s1.setName("Service A");
Service s2 = new Service();
s2.setActive(true);
s2.setName("Service B");
myServices.add(s1);
myServices.add(s2);
Customer cust = new Customer();
cust.setServices(myServices);
sampleHandler.marshal(path, cust);
Customer backAgain = sampleHandler.unmarshal(path);
}
}
Please let me know if this works for you. Happy coding :)
如果这对你有用,请告诉我。快乐编码:)