Reputation: 32331
<BADFM>
<Order >
<Ord OrdQty="1" faft="O">
<Bag pakka="1" DSE="C"/>
</Ord>
</Order>
</BADFM>
=================
@XmlRootElement(name="BADFM")
public class Message
{
@XmlElement(name="Order")
private MobBlock mob;
public MobBlock getMob() {
return mob;
}
public void setmob(MobBlock mob) {
this.mob = mob;
}
}
==================
public class MobBlock {
private Ord ord;
@XmlElement(name="Ord")
public Ord getOrd() {
return ord;
}
public void setOrd(Ord ord) {
this.ord = ord;
}
}
===================
public class Ord {
private Bagger Bagger;
@XmlElement(name="Bag")
public Bagger getBagger() {
return Bagger;
}
public void setBagger(Bagger Bagger) {
this.Bagger = Bagger;
}
}
=======================
public class Bagger {
private String dsc;
@XmlAttribute(name = "DSE")
public String getDSE() {
return dsc;
}
public void setDSE(String dsc) {
this.dsc = dsc;
}
}
========================
Its returning me null , when i tried to access
System.out.println(m.getMob().getOrd().getBagger().getDSE());
Could anybody please tell me what is the problem ?
This is my Parser class
public class BadFmParser
{
public static AbstractMessageT parse(String Str) throws Exception {
private static JAXBContext jc = null;
jc = JAXBContext.newInstance("mypackage");
Unmarshaller um = jc.createUnmarshaller();
BADFM badfmMessage = (BADFM) um.unmarshal(new StringReader(Str));
JAXBElement<? extends Message> element = badfmMessage.getMessage();
return element.getValue();
}
i am calling it this way
Message message = BADFMParser.parse(XMLFile);
Upvotes: 0
Views: 178
Reputation: 12019
This bit of your code...
BADFM badfmMessage = (BADFM) um.unmarshal(new StringReader(Str));
JAXBElement<? extends Message> element = badfmMessage.getMessage();
... is somewhat puzzling. You're getting an Object from the Unmarshaller, cast it to BADFM and then get a JAXBElement from it? What is this BADFM class?
I've taken your Message, MobBlock, Ord and Bagger classes, placed them in a package and then created this Main class in it:
package jaxb1;
import java.io.Reader;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
public class Main {
public static void main(String[] args) throws Exception {
final JAXBContext context =
JAXBContext.newInstance(
Bagger.class,
Ord.class,
MobBlock.class,
Message.class
);
final Unmarshaller unmarshaller = context.createUnmarshaller();
final Reader reader =
new StringReader("<?xml version=\"1.0\"?>\r\n"
+ "<BADFM>\r\n"
+ "<Order >\r\n"
+ "<Ord OrdQty=\"1\" faft=\"O\">\r\n"
+ "<Bag pakka=\"1\" DSE=\"C\"/>\r\n"
+ "</Ord>\r\n"
+ "</Order>\r\n"
+ "</BADFM>");
final Object o = unmarshaller.unmarshal(reader);
final Message m = (Message)o;
System.out.println(m.getMob().getOrd().getBagger().getDSE());
}
}
The result works fine. It outputs C
when run.
Upvotes: 0
Reputation: 8169
Where are you getting 'm' from? Post your code for unmarshalling.
As a side note, if you created classes manually chances are that you did something wrong. Easiest way to do sanity check is to create schema for your XML (if you don't have it then you should, for many reasons) and generate Java classes with 'xjc' (comes with JDK). Then you could either use these classes directly to Marshal/Unmarshal or just inspect them to see how they're different from yours.
Upvotes: 1