Utpal
Utpal

Reputation: 9

XML parser using jaxb

How do I get the object of a nested xml using jaxb. I have the following XML -

<?xml version="1.0" ?>
    <file>
    <markups>
    <markup>
        <author>author</author>
        <name>3w2fg</name>
        <source>Mobile_iPad</source>
        <createdOn>2013-05-20T11:22:23</createdOn>
        <entities>
            <entity>
                <entityWdth>209</entityWdth>
                <entityColor>
                    <red>127.5</red>
                    <green>0</green>
                    <blue>127.5</blue>
                </entityColor>
                <entityFillColor>
                    <red>227.5</red>
                    <green>0</green>
                    <blue>327.5</blue>
                </entityFillColor>
                <entityRadian>0</entityRadian>
                <entityEndY>304</entityEndY>
                <entityStX>438</entityStX>
                <entityTypeCode>7</entityTypeCode>
                <entityPageNo>1</entityPageNo>
                <entityHt>183</entityHt>
                <entityCenterX>542.5</entityCenterX>
                <entityName>Rectangle</entityName>
                <entityStY>121</entityStY>
                <entityEndX>647</entityEndX>
                <entityCenterY>212.5</entityCenterY>
            </entity>
        </entities>
    </markup>
</markups>
<name>7987ab12-4915-49e5-8bbd-f98d6054ef6b.JPG</name>
<fileName>IMG_0008.JPG</fileName>
</file>

I am using jaxb to unmarshal this as under -

JAXBContext jbContext = JAXBContext.newInstance(com.arc.markupinfo.generated.File.class);
com.arc.markupinfo.generated.ObjectFactory factory = new    com.arc.markupinfo.generated.ObjectFactory();
com.arc.markupinfo.generated.File fileObj = factory.createFile();
Unmarshaller unmarshaller = jbContext.createUnmarshaller();
fileObj = (com.arc.markupinfo.generated.File) unmarshaller.unmarshal(new   File(xmlLocation));
fileObj.getFileName();

The object is created with all the values except the entityColor.Red ... and entityFillColor.Red.... These values come as 0,0,0 while the xml shows that it has proper values

Upvotes: 0

Views: 163

Answers (1)

Ilya
Ilya

Reputation: 29703

entityColor.Red ... and entityFillColor.Red ... have type int (Integer).
Use double (Double), float (Float) or String for you colors in Color object.

@XmlAccessorType(XmlAccessType.FIELD)
public class Entity 
{
   //...
   private Color entityColor;
   private Color entityFillColor;
   //...
}  

@XmlAccessorType(XmlAccessType.FIELD)
public class Color 
{
   private double red;   // or float, or String
   private double green; // or float, or String
   private double blue;  // or float, or String
}  

Also you can unmarshal your example is simplest way:

File file = javax.xml.bind.JAXB
   .unmarshal(new java.io.File(xmlLocation),File.class);

Upvotes: 1

Related Questions