Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jaxb unmarshalling with namespace

This is my xml, need to convert it into java. I had used jaxb

<?xml version="1.0"?>
<lm:order Id="PLG24M240U" JD="" aCount="2" SUCount="1" xmlns:lm="http://www.ae.com/Event/Load">
  <lm:master>
   <lm:ID>3</lm:ID>
    <lm:Number>313</lm:Number>
    <lm:ANumber>323</lm:ANumber>     
  </lm:master>
  <lm:detail>
    <lm:ID>3</lm:ID>
    <lm:Number>3131</lm:Number>
    <lm:ANumber>3232</lm:ANumber>      
  </lm:detail>
 <lm:detail>
    <lm:ID>3</lm:ID>
    <lm:Number>3131</lm:Number>
    <lm:ANumber>3232</lm:ANumber>    
  </lm:detail>
  <lm:detail>
    <lm:ID>3</lm:ID>
    <lm:Number>313</lm:Number>
    <lm:ANumber>323</lm:ANumber>    
  </lm:detail>
</lm:order>

And throwing the following exception javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.ae.com/Event/Load", local:"Order"). Expected elements are <{}lm:Order>

This is my unmarshalling code

jaxbContext = JAXBContext.newInstance(Order.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Order order = (Order) jaxbUnmarshaller.unmarshal(file);
System.out.println(order );

Order Pojo class

import java.util.ArrayList;
import java.util.List;

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;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "lm:Order")
public class OrderPay {
    @XmlAttribute
    private String Id;
    @XmlAttribute
    private String JD;
    @XmlAttribute
    private String aCount;
    @XmlAttribute
    private String pCount;
    /*@XmlElement
    private Master master;
    @XmlElement
    private List<Detail> details = new ArrayList<Detail>();*/

}

Can you please help me in reading also, currently reading through file, need to read as an XML String.

like image 536
Rosh Avatar asked Sep 13 '14 04:09

Rosh


People also ask

How do you Unmarshal XML without namespace?

Unmarshaller. unmarshal(rootNode, MyType. class); you don't need to have a namespace declaration in the XML, since you pass in the JAXBElement that has the namespace already set.

What is JAXB unmarshalling?

The JAXB Unmarshaller interface is responsible for governing the process of deserializing the XML data to Java Objects. The unmarshalling to objects can be done to variety of input sources.

How do you Unmarshal string JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.


Video Answer


1 Answers

The namespace attribute xmlns:lm="http://www.ae.com/Event/Load" might be the culprit here. In order to specify the namespace prefix, you can add the @XmlSchema annotation to a package-info.java file like this:

@XmlSchema(
    namespace="http://www.ae.com/Event/Load",
    elementFormDefault=XmlNsForm.QUALIFIED),
    xmlns={@XmlNs(prefix="lm", namespaceURI="http://www.ae.com/Event/Load")})  

package your.package;
import javax.xml.bind.annotation.*;
like image 198
ivan.sim Avatar answered Sep 23 '22 17:09

ivan.sim