【问题标题】:XMLDecoder >> java.lang.IllegalArgumentException: Unsupported elementXMLDecoder >> java.lang.IllegalArgumentException:不支持的元素
【发布时间】:2019-08-10 13:38:54
【问题描述】:

遇到这个错误

java.lang.IllegalArgumentException:不支持的元素:网络

来自这个示例 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
    <net>
       <node label="A">
        ...
       </node>
       <node label="B">
        ...
       </node>
       <node label="C">
        ...
       </node>
    </net>

使用这些 java 代码行

    ...
    FileInputStream file = new FileInputStream("example.xml");
    XMLDecoder decoder = new XMLDecoder(file);
    Object decodedResistors = (Object) decoder.readObject();
    file.close();
    ...

【问题讨论】:

    标签: java xml deserialization xml-deserialization illegalargumentexception


    【解决方案1】:

    不要将java.beans.XMLDecoder 用于反序列化自定义XML 有效负载。它不是为此而设计的。阅读文章Long Term Persistence of JavaBeans Components: XML Schema。它包含一些示例 XML 有效载荷,可以通过 XMLDecoder 反序列化:

    <?xml version="1.0" encoding="UTF-8" ?>
    <java version="1.4.0" class="java.beans.XMLDecoder">
        <void id="myController" property="owner"/>
        <object class="javax.swing.JButton">
            <void method="addActionListener">
                <object class="java.beans.EventHandler" method="create">
                    <class>java.awt.event.ActionListener</class>
                    <object idref="myController"/>
                    <string>doIt</string>
                </object>
            </void>
        </object>
    </java>
    

    如果您需要反序列化自定义XML,请使用JAXBJackson XML。您需要创建一个带有JAXB 注释的POJO 模型:

    @XmlRootElement(name = "net")
    @XmlAccessorType(XmlAccessType.FIELD)
    class Net {
    
        @XmlElement(name = "node")
        private List<Node> nodes;
    
        // getters, setters, toString
    }
    
    @XmlAccessorType(XmlAccessType.FIELD)
    class Node {
    
        @XmlAttribute
        private String label;
    
        // getters, setters, toString
    }
    

    示例用法:

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import java.io.File;
    import java.util.List;
    
    public class JaxbApp {
    
        public static void main(String[] args) throws Exception {
            File xmlFile = new File("./resource/test.xml").getAbsoluteFile();
    
            JAXBContext jaxbContext = JAXBContext.newInstance(Net.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Object net = unmarshaller.unmarshal(xmlFile);
            System.out.println(net);
        }
    }
    

    打印:

    Net{nodes=[Node{label='A'}, Node{label='B'}, Node{label='C'}]}
    

    另见:

    【讨论】:

    猜你喜欢
    • 2018-07-20
    • 2016-07-09
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 2018-07-05
    • 2016-09-20
    相关资源
    最近更新 更多