473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

xml to java

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
Jul 20 '05 #1
3 2720
timothy ma and constance lee (ti******@shaw.ca) 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.
Jul 20 '05 #2
yf***@vtn1.victoria.tc.ca (Malcolm Dew-Jones) wrote in message news:<41******@news.victoria.tc.ca>...
timothy ma and constance lee (ti******@shaw.ca) 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[i].getType();
nodeList[i].getKey();
nodeList[i].getValue();
}

I think it's DOM based, not SAX.

Later,
Paul

--
Paul Webster
"Marg! Marg! The Doll's tryin' to kill me and the toaster's been
laughin'
at me!!" - Homer
Jul 20 '05 #3
<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/techni...Services/jaxb/

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

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

HTH,
AT
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
0
by: mailkhurana | last post by:
Hii , I am trying to use a type 2 driver to connect to DB2 0n AIX 5 I have a small java test to class to establish a conneciton with the db .. I am NOT using WAS or any appserver When I try to...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
0
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build...
1
by: jaimemartin | last post by:
hello, I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.