【发布时间】:2015-03-12 09:36:21
【问题描述】:
无法将表单节点转换为对象
我无法解组以下 XML 中的表单节点:
<?xml version="1.0" encoding="UTF-8"?>
<start>
<action>
<application>
<name>sarat</name>
</application>
<forms>
<form>
<name>flexMobile</name>
<type>flex</type>
<channel>mobile</channel>
</form>
<form>
<name>flexMobile1</name>
<type>flex1</type>
<channel>mobile1</channel>
</form>
</forms>
</action>
</start>
感谢表单上有指针和表单节点转换为对象
Start.js
package com.jaxb;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Start {
Action action;
@XmlElement
public Action getAction(){
return action;
}
public void setAction(Action action){
this.action = action;
}
}
Action.js
package com.jaxb;
import javax.xml.bind.annotation.*;
import java.util.List;
public class Action {
private Application application;
private List<Form> forms;
public Action(){}
public Action(Application application, List<Form> forms){
super();
this.application = application;
this.forms = forms;
}
@XmlElement
public Application getApplication(){
return this.application;
}
public void setApplication(Application application){
this.application = application;
}
@XmlElement
public List<Form> getForms(){
return forms;
}
public void setForms(List<Form> forms){
this.forms = forms;
}
}
应用程序.js
package com.jaxb;
import javax.xml.bind.annotation.XmlElement;
public class Application {
private String name;
@XmlElement
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
Forms.js
package com.jaxb;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class Forms {
private List<Form> form;
@XmlElement
public List<Form> getForm(){
return form;
}
public void setForms(List<Form> form){
this.form = form;
}
}
Form.js
package com.jaxb;
import javax.xml.bind.annotation.*;
public class Form {
private String name;
private String type;
private String channel;
private Form(){}
private Form(String name, String type, String channel){
super();
this.name = name;
this.type = type;
this.channel = channel;
}
@XmlElement
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@XmlElement
public String getType(){
return type;
}
public void setType(String type){
this.type = type;
}
@XmlElement
public String getChannel(){
return channel;
}
public void setChannel(String channel){
this.channel = channel;
}
}
我无法构造 Forms 类来将 xml 转换为对象,如果有任何建议,不胜感激
Parser.js
package com.jaxb;
import java.io.File;
import java.util.List;
import javax.xml.bind.*;
public class Parse {
public static void main(String[] args) {
try {
File file = new File("data.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Start.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Start que= (Start) jaxbUnmarshaller.unmarshal(file);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(que, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
当您填充对象模型并将其编组出来时,生成的 XML 是什么样的?
-
更新了 Action.java,下面是编组时的输出
sarat -
这是编组完全填充的对象模型还是编组未编组的对象的结果?
标签: java xml jaxb unmarshalling