Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Complex type to String unmarshalling

  • 07-03-2013 9:51pm
    #1
    Closed Accounts Posts: 6,075 ✭✭✭


    Can anyone tell me how to convert a complex object into a String in JAXB?

    Scenario: The Car objects below has a nested element called Person. The Person element has 2 fields. Does anyone know how to unmarshall the Person object into a single string? I do not need any other information, just the Person name.

    Is there a way to do this with JAXB?

    E.g.

    XML
    [HTML]<Car>
    <Owner>
    <Name>
    John Blogs
    </name>
    </owner>
    </Car>
    [/HTML]

    How do I create a Java object like the following:
    class Car{
        private String owner; //John Blogs
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    You need to create a class for each "containing" node, so Car, Owner and Name, car has an Owner member, Owner has a Name member (which is a String)

    Then you can just annotate each class
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Car {
    
        @XmlElement(name = "Owner")
        private Owner owner;
    
        getOwner();
        setOwner();
    }
    

    Then instantiate your JAXB Context (statically, you only need one of these as they are thread safe)
    JAXBContext ctx =    JAXBContext.newInstance(Car.class.getPackage().getName());
    
    Then actually unmarshall your XML into your pojo

    Unmarshaller u = ctx.createUnmarshaller();
            Car car  = (Car) u.unmarshal(new StringReader(xmlInput));
    

    You get the name by doing a car.getOwner().getName();



    /edit
    You cant (easily) create the object you describe because thats not the object that your XML naturally represents.

    If you have access to modify the XML then you could switch the name to be an attribute
    <Car ownerName="John Blogs"/>

    And the update the car class to have
    @XmlAttribute(name = "ownerName")
    private String ownerName


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    GreeBo wrote: »
    You need to create a class for each "containing" node, so Car, Owner and Name, car has an Owner member, Owner has a Name member (which is a String)

    Then you can just annotate each class
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Car {
    
        @XmlElement(name = "Owner")
        private Owner owner;
    
        getOwner();
        setOwner();
    }
    

    Then instantiate your JAXB Context (statically, you only need one of these as they are thread safe)
    JAXBContext ctx =    JAXBContext.newInstance(Car.class.getPackage().getName());
    
    Then actually unmarshall your XML into your pojo

    Unmarshaller u = ctx.createUnmarshaller();
            Car car  = (Car) u.unmarshal(new StringReader(xmlInput));
    

    You get the name by doing a car.getOwner().getName();



    /edit
    You cant (easily) create the object you describe because thats not the object that your XML naturally represents.

    If you have access to modify the XML then you could switch the name to be an attribute
    <Car ownerName="John Blogs"/>

    And the update the car class to have
    @XmlAttribute(name = "ownerName")
    private String ownerName

    Thanks for you reply. I do not have access to the XML as it's an external service. I decided to write a class that implements the DomHandler interface. This means when the Person element is unmarshalled my DomHandler class is invoked and can manipulated what is returned, in my case string.


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    I believe you can use XmlPath.

    Something like:
    class Car{
        @XmlPath("Owner/Name/text()")
        private String owner; //John Blogs
    }
    


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    I believe you can use XmlPath.

    Something like:
    class Car{
        @XmlPath("Owner/Name/text()")
        private String owner; //John Blogs
    }
    

    How does that work if I'm not using Eclipse MOXy?


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    How does that work if I'm not using Eclipse MOXy?

    Sorry, didn't cop that it was a non-standard attribute.


  • Advertisement
  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    I added Eclipse MOXy as a dependency and XMLPat worked.

    Thanks a million.


Advertisement