【问题标题】:JAXB class declaration for the below xml以下 xml 的 JAXB 类声明
【发布时间】: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


【解决方案1】:

您的 xml 和类之间不匹配。 xml 建议您拥有包含表单元素列表的对象表单。 您的 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 类。这样你就可以避免这种错误了

【讨论】:

  • 感谢您的建议,实际上代码在我为元素属性添加 name="forms" 时有效
【解决方案2】:

在 forms.js 中,添加 name 值后,代码工作

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2012-02-11
    相关资源
    最近更新 更多