【问题标题】:Marshaling exception when using jaxb使用 jaxb 时编组异常
【发布时间】:2013-05-27 03:48:37
【问题描述】:

我有 java 代码,我正在尝试使用 JAXB 解析它并将其作为 XML 文件存储在我的 PC 上,但我有一个编组异常:

Exception in thread "main" javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation]
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:103)
    at xml.ConfList.addToList(ConfList.java:29)
    at xml.Tester.work(Tester.java:34)
    at xml.Tester.main(Tester.java:16)
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:237)
    at com.sun.xml.internal.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot(LeafBeanInfoImpl.java:126)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
    ... 5 more
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)

我正在使用的 JAXB 代码:

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
    public class ConfList {

        private static final String fileName = "Source.xml";
        List<String> xmlConfList;
        private Object object;

        public ConfList(Object object){
            this.object = object;
        }

        public void addToList() throws IOException, JAXBException {

            File file = new File(fileName);
            JAXBContext jaxbContext = JAXBContext.newInstance(XmlConf.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(object, file); // i got the exception here
            jaxbMarshaller.marshal(object, System.out);

        }
    }

我使用的主要类:

import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.SAXException;

public class Tester{

    public static void main (String [] args) throws XPathExpressionException, IOException, ParserConfigurationException, SAXException, TransformerException, JAXBException{

        work();

    }

    public static void work () throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, TransformerException, JAXBException{


        String surl = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=Cairo&format=xml&num_of_days=1&key=wd63kxr294rcgvbynhaf2z4r";
        XmlSource xml = new XmlSource(surl);
        xml.validate();
        xml.load();
        ArrayList<String> paths = xml.getAllPaths();

        for(String path : paths){
        System.out.println(path);
        }

        ConfList v = new ConfList(xml.getXml());
        v.addToList();

    }
}

XmlConf 类:

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "XmlSource")
public class XmlConf {

    private URL url;
    private List<String> path = new ArrayList<String>();
    private String urlp;
    private Map<String, String> parameters;
    private String host;


    public URL getUrl() {
        return url;
    }
    @XmlAttribute(name = "URL")
    public void setUrl(URL url) {
        this.url = url;
    }
    @XmlElement
    public List<String> getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path.add(path);
    }
    @XmlElement
    public void setUrlPath(String urlp){
        this.urlp = urlp;
    }
    public String getUrlPath(){
        return urlp;
    }
    public void setParameters(Map<String, String> parameters){
        this.parameters = parameters;
    }
    public Map<String, String> getParameters(){
        return parameters;
    }
    public void setHostName(String host){
        this.host = host;
    }
    public String getHostName(){
        return host;
    }
}

请注意,所有(XmlSource 类)方法都正确运行。

【问题讨论】:

    标签: java xml exception jaxb marshalling


    【解决方案1】:

    下面是一些示例代码,它将一些 XML 解组为 XmlConf 的实例,然后将其编组回 XML。

    演示

    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(XmlConf.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum16762200/input.xml");
            XmlConf xmlConf = (XmlConf) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(xmlConf, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <XmlSource URL="http://www.example.com">
        <parameters>
            <entry>
                <key>A</key>
                <value>a</value>
            </entry>
            <entry>
                <key>B</key>
                <value>b</value>
            </entry>
        </parameters>
        <path>foo</path>
        <urlPath>bar</urlPath>
    </XmlSource>
    

    【讨论】:

    • 我运行了你的代码但得到了那个异常:线程“main”javax.xml.bind.UnmarshalException中的异常
    • @MuhammedRefaat - 你得到了什么UnmarshalException(在你的问题中你得到了一个MarshalException。我能够运行我发布的代码。
    • 是的,它起作用了,只是我传递的对象类型有问题。
    猜你喜欢
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多