xml to java

  • Thread starter timothy ma and constance lee
  • Start date
T

timothy ma and constance lee

i have an xml file like

<node type="Service" key="1">
<value>Title</value>
</node>
<node type="Service" key="2">
<value>CA</value>
</node>

Can java parse through the xml file to create an object like

VO.getNodeType
VO.getKey
VO.getValue
 
M

Malcolm Dew-Jones

timothy ma and constance lee ([email protected]) wrote:
: i have an xml file like

: <node type="Service" key="1">
: <value>Title</value>
: </node>
: <node type="Service" key="2">
: <value>CA</value>
: </node>

: Can java parse through the xml file to create an object like

: VO.getNodeType
: VO.getKey
: VO.getValue

two ways come to mind

-1-

parse the xml using a dom parser (java has a "few" of these available)

create a class that uses the dom parser to load the document into memory.

each access method in your class, e.g. getNodeType, is simply a front end
for the set of standard dom calls that return the desired information.

-2-

create a VO class that has the necessary new/get/set methods to create a
vo object

parse the xml using a sax parser (java has a "few" of these available),
and provide a sax content handler that does the following

a. creates a VO object when a <node > tag is found (current_VO = new VO)

b. uses the appropriate VO set methods to define the contents of the vo
object as each bit of information is available.
current_VO->setValue(-value-data-returned-by-sax-)


-3-

I bet there are prepackaged ways to do this, so I'm interested to hear
what other suggestions are made.
 
P

Paul News

timothy ma and constance lee ([email protected]) wrote:
: i have an xml file like

: <node type="Service" key="1">
: <value>Title</value>
: </node>
: <node type="Service" key="2">
: <value>CA</value>
: </node>
[... snip ...]
-3-

I bet there are prepackaged ways to do this, so I'm interested to hear
what other suggestions are made.

I got the impression there are a couple of packages to do this.

We've been using XmlBeans, http://xmlbeans.apache.org

You can create a simple XML schema that describes your elements, then
use the XmlBeans generated API to read and access you document.

example:
NodesDocument doc = NodesDocument.Factory.parse(inputStream);
Nodes nodes = doc.getNodes();
Node[] nodeList = nodes.getNodeArray();
for (int i=0; i<nodeList.length; ++i) {
nodeList.getType();
nodeList.getKey();
nodeList.getValue();
}

I think it's DOM based, not SAX.

Later,
Paul
 
A

Andrew Tyson

<snip>

Can java parse through the xml file to create an object like
VO.getNodeType
VO.getKey
VO.getValue

You might want to consider the Java Architecture for XML Binding JAX-B.

If you create an XML Schema for the XML document, then you can use the
associated binding compiler to auto generate equivalent Java classes that
represent
the elements of your XML document. It is then a trivial matter to create a
Java IO stream
to either unmarshal an XML document to the equivalent object heirarchy, or
marshal an object
heirarchy to the eqivalent XML document structure. Check it out;

http://java.sun.com/developer/technicalArticles/WebServices/jaxb/

In addition to the Sun JAX-B RI there is also an Apache implementation;

http://ws.apache.org/jaxme/

HTH,
AT
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top