1

Got this error

java.lang.IllegalArgumentException: Unsupported element: net

from this example xml file

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

with these java code lines

    ...
    FileInputStream file = new FileInputStream("example.xml");
    XMLDecoder decoder = new XMLDecoder(file);
    Object decodedResistors = (Object) decoder.readObject();
    file.close();
    ...
B0r1
  • 400
  • 3
  • 15

1 Answers1

3

Do not use java.beans.XMLDecoder for deserialisation custom XML payloads. It was not designed for that. Read article Long Term Persistence of JavaBeans Components: XML Schema. It contains some example XML payloads which can be deserialised back by 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>

If you need to deserialise custom XML use JAXB or Jackson XML. You need to create a POJO model with JAXB annotations:

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

Example usage:

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

prints:

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

See also:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • I got now an exception in "JAXBContext jaxbContext = JAXBContext.newInstance(Net.class);" Exception in thread "main" javax.xml.bind.JAXBException - with linked exception: [java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory] I have created the class "net" – B0r1 Mar 21 '19 at 08:32
  • @B0r1, probably you need to add some dependencies: [Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.model.annotation.AnnotationReader](https://stackoverflow.com/questions/36557308/caused-by-java-lang-classnotfoundexception-com-sun-xml-bind-v2-model-annotatio) – Michał Ziober Mar 21 '19 at 08:35