Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2005

Tip: Looking for answers? Try searching our database.

JAXB:Parsing XML Data

Thread view: 
pitthekid - 19 Sep 2005 13:23 GMT
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.
Roedy Green - 20 Sep 2005 07:05 GMT
> <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!
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 20 Sep 2005 07:32 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

angdis - 20 Sep 2005 13:49 GMT
> 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.
stylusstudio@gmail.com - 20 Sep 2005 15:13 GMT
Hi - apologies for the commercial interruption but Stylus Studio also
provides tools to generate and edit XML Schemas:

Generate XML Schemas: http://www.stylusstudio.com/autogen_xsd.html
XML Schema Editor: http://www.stylusstudio.com/xml_schema_editor.html

Sincerely,
The Stylus Studio Team
http://www.stylusstudio.com
Roedy Green - 21 Sep 2005 23:04 GMT
On 20 Sep 2005 07:13:51 -0700, stylusstudio@gmail.com wrote or quoted

>Generate XML Schemas: http://www.stylusstudio.com/autogen_xsd.html
>XML Schema Editor: http://www.stylusstudio.com/xml_schema_editor.html

Thanks. I have added those links to my list of XML tools posted at
http://mindprod.com/jgloss/xml.html

I hope someday this XML craze will evolve into a new tighter
underlying format, one that understands age old data types like binary
ints, floats and doubles, with low and high bounds defined in the
schema.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 21 Sep 2005 22:57 GMT
>I find that it is best to work with a tool like XMLspy (there is a free
>edition) to generate schemas.

There is a free "home" version. The enterprise edition is over $1200
per seat!

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

https://shop.altova.com/product.asp?catalog%5Fname=V2005R3C3%5Fshop&category%5Fn
ame=XMLSPY&product%5Fid=S05E


Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

gerrards8@yahoo.com - 22 Sep 2005 05:08 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.