0

I want to create constructor which takes xml as string and all variables would be fill from that. That xml is created by XStream so I think that something like this would work but don´t know what to add to left side:

    public xmlpacket(String xml)
    {
        XStream xstream = new XStream(new DomDriver());
       .... =  (xmlpacket)xstream.fromXML(xml);
    }

Thank you

1

3 Answers 3

1

You can make use of a static method.

public static XMLPacket unmarshall(String xml) {
    XStream xstream = new XStream(new DomDriver());

    return (XMLPacket)xstream.fromXML(xml);

}

Notice how I renamed your class. Now it adheres to Java Naming Conventions.

Sign up to request clarification or add additional context in comments.

Comments

1

If you've created the classes you need and the xstream aliases for the classes then

XMLPacket packet = (XMLPacket)xstream.fromXML(xml);

But you should probably create a method for this and not do it in the constructor.

Comments

0

XStream will create the instance of the object for you. So unless you want to copy all the attributes of the XStream-created packet to the packet you're constructing, it makes no sense to do that in a constructor. Create a factory method instead:

public static XmlPacket fromXml(String xml) {
    XStream xstream = new XStream(new DomDriver());
    return (XmlPacket) xstream.fromXML(xml);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.