JAXB:Parsing XML Data

P

pitthekid

Hello,

I discovered JAXB to Parse XML files. It is a fine thing and I tested
the examples of jwsdp-1.6/jaxb. All went ok, I created a xsd, a
build.xml which then created the nessecary Java Files.

Afterwards I created a simple xml file.


This is the xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="1.0">

<xs:element name="AERORDROME">
<xs:complexType>
<xs:sequence>
<xs:element name="RWY_ALOC_KEY" type="xs:int"/>
<xs:element name="RWY_ID" type="xs:int"/>
<xs:element name="RWY_LAST_UPDATE" type="xs:string"/>
<xs:element name="RWY_OPERATOR" type="xs:string"/>
<xs:element name="RWY_ILS_CAT" type="xs:string"/>
<xs:element name="RWY_LOC_IDENT" type="xs:string"/>
<xs:element name="RWY_LOC_TYPE" type="xs:string"/>
<xs:element name="RWY_LOC_FREQ" type="xs:int"/>
<xs:element name="RWY_LOC_DME_CHAN" type="xs:string"/>
</xs:sequence>

</xs:complexType>
</xs:element>


</xs:schema>





this is an xml file:



<AERORDROME >
<RWY_ALOC_KEY>1</RWY_ALOC_KEY>
<RWY_ID>2</RWY_ID>
<RWY_LAST_UPDATE>RWY_LAST_UPDATE781-555-1212</RWY_LAST_UPDATE>
<RWY_OPERATOR>RWY_OPERATOR781-555-1212</RWY_OPERATOR>
<RWY_ILS_CAT>RWY_ILS_CAT781-555-1212</RWY_ILS_CAT>
<RWY_LOC_IDENT>RWY_LOC_IDENT781-555-1212</RWY_LOC_IDENT>
<RWY_LOC_TYPE>RWY_LOC_TYPE781-555-1212</RWY_LOC_TYPE>
<RWY_LOC_FREQ>4711</RWY_LOC_FREQ>
<RWY_LOC_DME_CHAN>RWY_LOC_DME_CHAN781-555-1212</RWY_LOC_DME_CHAN>
</AERORDROME>

Withinn a Java application I could read the Data of the xml file:
public static void main( String[] args ) {
try {
// create a JAXBContext capable of handling classes
generated into
// the example package
JAXBContext jc = JAXBContext.newInstance( "example" );

// create an Unmarshaller
Unmarshaller u = jc.createUnmarshaller();

// unmarshal a FooBar instance document into a tree of Java
content
// objects composed of classes from the example package.
AERORDROME aerodrome = (AERORDROME)u.unmarshal( new
FileInputStream( "fsi.xml" ) );
int RWY_ALOC_KEY = aerodrome.getRWYALOCKEY();
String RWY_LAST_UPDATE = aerodrome.getRWYLASTUPDATE();
String RWY_OPERATOR = aerodrome.getRWYOPERATOR();
String RWY_ILS_CAT = aerodrome.getRWYILSCAT();
String RWY_LOC_IDENT = aerodrome.getRWYLOCIDENT();
String RWY_LOC_TYPE = aerodrome.getRWYLOCTYPE();
int RWY_LOC_FREQ = aerodrome.getRWYLOCFREQ();
String RWY_LOC_DME_CHAN = aerodrome.getRWYLOCDMECHAN();

System.out.println("RWY_ALOC_KEY[" + RWY_ALOC_KEY + "]");
System.out.println("RWY_LAST_UPDATE[" + RWY_LAST_UPDATE +
"]");
System.out.println("RWY_OPERATOR[" + RWY_OPERATOR + "]");
System.out.println("RWY_ILS_CAT[" + RWY_ILS_CAT + "]");
System.out.println("RWY_LOC_IDENT[" + RWY_LOC_IDENT + "]");
System.out.println("RWY_LOC_TYPE[" + RWY_LOC_TYPE + "]");
System.out.println("RWY_LOC_FREQ[" + RWY_LOC_FREQ + "]");
System.out.println("RWY_ALOC_KEY[" + RWY_ALOC_KEY + "]");
System.out.println("RWY_LOC_DME_CHAN[" + RWY_LOC_DME_CHAN +
"]");
....
}


Now I would like to read from one big xml file with a structure like
<AERORDROMES >
<AERORDROME >
<RWY_ALOC_KEY>1</RWY_ALOC_KEY>
<RWY_ID>2</RWY_ID>
<RWY_LAST_UPDATE>RWY_LAST_UPDATE781-555-1212</RWY_LAST_UPDATE>
<RWY_OPERATOR>RWY_OPERATOR781-555-1212</RWY_OPERATOR>
<RWY_ILS_CAT>RWY_ILS_CAT781-555-1212</RWY_ILS_CAT>
<RWY_LOC_IDENT>RWY_LOC_IDENT781-555-1212</RWY_LOC_IDENT>
<RWY_LOC_TYPE>RWY_LOC_TYPE781-555-1212</RWY_LOC_TYPE>
<RWY_LOC_FREQ>4711</RWY_LOC_FREQ>
<RWY_LOC_DME_CHAN>RWY_LOC_DME_CHAN781-555-1212</RWY_LOC_DME_CHAN>
</AERORDROME>
<AERORDROME >
<RWY_ALOC_KEY>1</RWY_ALOC_KEY>
<RWY_ID>3</RWY_ID>
<RWY_LAST_UPDATE>RWY_LAST_UPDATE781-555-1212</RWY_LAST_UPDATE>
<RWY_OPERATOR>RWY_OPERATOR781-555-1212</RWY_OPERATOR>
<RWY_ILS_CAT>RWY_ILS_CAT781-555-1212</RWY_ILS_CAT>
<RWY_LOC_IDENT>RWY_LOC_IDENT781-555-1212</RWY_LOC_IDENT>
<RWY_LOC_TYPE>RWY_LOC_TYPE781-555-1212</RWY_LOC_TYPE>
<RWY_LOC_FREQ>4711</RWY_LOC_FREQ>
<RWY_LOC_DME_CHAN>RWY_LOC_DME_CHAN781-555-1212</RWY_LOC_DME_CHAN>
</AERORDROME>
.....
<AERORDROMES >

How can I realize this, or better how can I parse it?

Thanks for help.

Yours, Pitthekid.
 
R

Roedy Green

<RWY_LOC_IDENT>RWY_LOC_IDENT781-555-1212</RWY_LOC_IDENT>

why would you not code that as:

<RWY_LOC_IDENT>781-555-1212</RWY_LOC_IDENT>

Saying it twice is redundant enough, but three times! ouch!
 
R

Roedy Green

I discovered JAXB to Parse XML files. It is a fine thing and I tested
the examples of jwsdp-1.6/jaxb. All went ok, I created a xsd, a
build.xml which then created the nessecary Java Files.

I had a quick look at JAXB and I was underwhelmed. But then I detest
XML so strongly, that anything associated with it is like the taste of
bread mould slathered with lard.

You have to put your schema into the generator and it should generate
you the source for some java classes, with string fields and
references to other classes that parallels the XML structure. You have
to compile those classes. Among them will be an Aerodrome class. (You
might choose your xml tags to have nice Java class or field names).
Presumably then the unmarshaller will produce you a reference to an
Aerodrome object complete with unmashalled fields with one call. You
don't unmarshall field by field.

Think of JAXB as sort of like a hobbled, wasteful serialization scheme
that reads and writes XML format.
 
A

angdis

Now I would like to read from one big xml file with a structure like
[... simple xml which conforms to an xsd that would be different from first one ...]

How can I realize this, or better how can I parse it?


Change your schema so it matches the xml file you want to consume,
generate the java code automatically and you are done.

I find that it is best to work with a tool like XMLspy (there is a free
edition) to generate schemas.
 
R

Roedy Green

G

gerrards8

Roedy said:
What on earth would a "home" user need with such a tool?

Balancing checkbooks, as demonstrated by the JAXB docs..

Not sure if it grinds coffee beans though, as no official standard has
been produced by OASIS for that, at least not yet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top