1

I m trying to convert XML file into Java Object using Jaxb unmarshalling.

                public static void main(String[] args) {
                        String input =  "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">"+
                                            " <key>1</key>" +
                                            "<income>100.335</income>" +
                                        "</project>" ;
        NexusClient c1 = new NexusClient();
                        c1.getObject(input);
                    }
  /*********/  
        public boolean getObject(String input) {
            InputSource inputSource = new InputSource(new StringReader(input));
            System.out.println(inputSource);

            try {
                JAXBContext jaxbContext = JAXBContext
                        .newInstance(mavenEntity.class);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                mavenEntity mavenObject = (mavenEntity) jaxbUnmarshaller
                        .unmarshal(inputSource);

                System.out.println("Success"+mavenObject.getIncome());
            } catch (JAXBException e) {
                System.out.println("Unable to parse the XML Context");
                e.printStackTrace();
                return false;
            }

            return true;
        }

I m facing an issue while trying to extract "Income" tag information. I couldn't extract correct values using Jaxb. My pojo class is :

@XmlRootElement(name = "project", namespace = "http://maven.apache.org/POM/4.0.0")
@XmlAccessorType(XmlAccessType.FIELD)
public class mavenEntity {

    @XmlElement(name = "key", type = String.class)
    private String key;

    @XmlElement(name = "income", type = String.class)
    private String income;


    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }


    public String getIncome() {
        return income;
    }
    public void setIncome(String income) {
        this.income = income;
    }
}

I m getting Null as output for any tag in XML. I guess there is some problem with my name space in XML Annotation. But I really don't understand what it is. Before posting this, I did some groundwork by referring to few links similar to this But still my result is incorrect. Can some one help me out.

3
  • Sorry but...what and where c1 is?! Commented Dec 9, 2013 at 15:49
  • Also update with what's actually happening, e.g., what's the fail mode? Commented Dec 9, 2013 at 15:54
  • Fail mode is, I m getting value as NULL for System.out.println("Success"+mavenObject.getIncome()); Commented Dec 9, 2013 at 15:58

2 Answers 2

2

The namespace qualification in your model does not match the document. Instead of specifying the namespace on @XmlRootElement and all instances of @XmlElement you can specify the namespace qualification at the package level using @XmlSchema.

package-info.java

@XmlSchema( 
    namespace = "http://maven.apache.org/POM/4.0.0", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package org.example.foo;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

mavenEntity.java

I have removed the unnecessary annotations from this class (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html).

package org.example.foo;

import javax.xml.bind.annotation.XmlSchema;

@XmlRootElement(name = "project")
@XmlAccessorType(XmlAccessType.FIELD)
public class mavenEntity {

    private String key;

    private String income;

}

For More Information

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

Comments

1

You will need to add namespace to your @XmlElement annotated fields too

@XmlElement(name = "key", namespace = "http://maven.apache.org/POM/4.0.0")
private String key;

@XmlElement(name = "income", namespace = "http://maven.apache.org/POM/4.0.0")
private String income;

That's because your root element has a particular namespace. Since the nested elements don't have namespace prefix, they are using the root's. I guess this is required by JAXB.

Some alternatives and/or explanations here and here.

4 Comments

It did work. .But I have a question.. why do I need to put Namespace for all the elements? My another question is, If from an XML file, I need to avoid few Fields for Java objects, what ignore annotation I need to use???
@asvikki I'm not exactly sure about the why. The articles I've linked might be of help. As for avoiding fields to be marshalled/unmarshalled you can use @XmlTransient.
Instead of specifying namespace on all of the @XmlElement annotations you can specify the namespace qualification once at the package level with @XmlSchema: stackoverflow.com/a/20475224/383861
@BlaiseDoughan I was waiting for you to post.

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.