无法将表单节点转换为对象
我无法在以下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;
}
}
Application.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();
}
}
}
答案 0 :(得分:0)
xml和类之间存在不匹配。 xml建议您使用包含Form元素列表的Object Forms。 你的java模型,直接在Action下有Form元素。 所以unmarshaller希望xml像这样:
<action>
<application>
<name>sarat</name>
</application>
<forms>
<name>flexMobile</name>
<type>flex</type>
<channel>mobile</channel>
</forms>
<forms>
<name>flexMobile1</name>
<type>flex1</type>
<channel>mobile1</channel>
</forms>
</action>
如果从xml开始,请生成xsd架构(可以为您执行此操作的网页)并使用它来生成jaxb类。这样你就可以避免这种错误
答案 1 :(得分:0)
在forms.js中,在添加名称值后,代码工作了
package com.jaxb;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class Forms {
private List<Form> form;
@XmlElement(name="forms)
public List<Form> getForm(){
return form;
}
public void setForms(List<Form> form){
this.form = form;
}
}