Need to retrieve all the node values of xml using DOM parser..pls help

I want to fetch each node value in this xml file:

<?xml version="1.0" encoding="UTF-8"?>

<Main>

<AAAAA>

<ES>ESValue</ES>

<EI>EIValue</EI>

</AAAAA>

<BBBBB>

<SIP>

<ST>STValue</ST>

<TB>TBValue</TB>

<PM>PMValue</PM>

<VIP>

<CARP>

<AN1>AN1Value</AN1>

<BN>BNValue</BN>

</CARP>

<DARP>

<SA>

<AN2>AN2Value</AN2>

<CN>CNValue</CN>

</SA>

</DARP>

</VIP>

</SIP>

</BBBBB>

</Main>

output should be the inner text values of diffrent nodes that contain some values..

i.e

output:

ESValue

EIValue

STValue

TBValue

PMValue

AN1Value

BNValue

AN2Value

CNValue

so that i can use thses node values and put it them in database...

[1051 byte] By [thinkmada] at [2007-11-27 2:10:07]
# 1
So which part are you having problems with?
sabre150a at 2007-7-12 2:01:27 > top of Java-index,Java Essentials,Java Programming...
# 2
http://www.zvon.org/xxl/XPathTutorial/General/examples.htmlCheck out example 3.
hunter9000a at 2007-7-12 2:01:27 > top of Java-index,Java Essentials,Java Programming...
# 3
Iam a newbie in DOM..want to fetch all the node values..pls help me with some DOM parser code in java
thinkmada at 2007-7-12 2:01:27 > top of Java-index,Java Essentials,Java Programming...
# 4

pls check the above xml file in proper redable order...I need to parse using DOM and fetch node values that are present...

<?xml version="1.0" encoding="UTF-8"?>

<Main>

<AAAAA>

<ES>ESValue</ES>

<EI>EIValue</EI>

</AAAAA>

<BBBBB>

<SIP>

<ST>STValue</ST>

<TB>TBValue</TB>

<PM>PMValue</PM>

<VIP>

<CARP>

<AN1>AN1Value</AN1>

<BN>BNValue</BN>

</CARP>

<DARP>

<SA>

<AN2>AN2Value</AN2>

<CN>CNValue</CN>

</SA>

</DARP>

</VIP>

</SIP>

</BBBBB>

</Main>

thinkmada at 2007-7-12 2:01:27 > top of Java-index,Java Essentials,Java Programming...
# 5
need reply urgently guys
thinkmada at 2007-7-12 2:01:27 > top of Java-index,Java Essentials,Java Programming...
# 6
any idea guys?
thinkmada at 2007-7-12 2:01:28 > top of Java-index,Java Essentials,Java Programming...
# 7
> any idea guys?Yeah, I gave you an idea in my last post. Use XPath, it's trivial to select all nodes in a document. If you don't know how to use xpath, then try googling for "java xpath".
hunter9000a at 2007-7-12 2:01:28 > top of Java-index,Java Essentials,Java Programming...
# 8

> any idea guys?

Yep, the parse method of a SAXParser accepts a DefaultHandler.

Extend such a handler and when its 'character' method is called do

something with that character array (it contains the CDATA text of

your xml nodes).

kind regards,

Jos (< hates xml)

JosAHa at 2007-7-12 2:01:28 > top of Java-index,Java Essentials,Java Programming...
# 9
only have to use DOM in my project...It's a simple code i know..but iam not getting the idea.
thinkmada at 2007-7-12 2:01:28 > top of Java-index,Java Essentials,Java Programming...
# 10
You have to visit each node of the DOM tree and extract all those named #text. A recursive approach is probably best.
sabre150a at 2007-7-12 2:01:28 > top of Java-index,Java Essentials,Java Programming...
# 11

> You have to visit each node of the DOM tree and

> extract all those named #text. A recursive approach

> is probably best.

I prefer my lazy bones approach (see previous reply). There's no need

to fire up an entire DOM thing for just that, i.e. a simple SAXParser can

do the job and it frees you from any recursive diddling too.

kind regards,

Jos

JosAHa at 2007-7-12 2:01:28 > top of Java-index,Java Essentials,Java Programming...
# 12

> > You have to visit each node of the DOM tree and

> > extract all those named #text. A recursive

> approach

> > is probably best.

>

> I prefer my lazy bones approach (see previous reply).

> There's no need

> to fire up an entire DOM thing for just that, i.e. a

> simple SAXParser can

> do the job and it frees you from any recursive

> diddling too.

>

Agreed, but the OP says he has to use DOM. Presumably the restriction applies because it is homework.

Sabre (<-- loves XML(some of the time))

sabre150a at 2007-7-12 2:01:28 > top of Java-index,Java Essentials,Java Programming...
# 13

> Agreed, but the OP says he has to use DOM. Presumably

> the restriction applies because it is homework.

Then the entire DOM thing it is. Drop in xpath and a bit of xslt and be

sure that all performance is lost for the posterity ;-)

> Sabre (<-- loves XML(some of the time))

men ...

kind regards,

Loretta.

JosAHa at 2007-7-12 2:01:28 > top of Java-index,Java Essentials,Java Programming...
# 14
> > kind regards,> > Loretta.Hi Loretta,Did you get too near the bread knife again? Sabre
sabre150a at 2007-7-12 2:01:28 > top of Java-index,Java Essentials,Java Programming...
# 15

> >

> > kind regards,

> >

> > Loretta.

>

> Hi Loretta,

>

> Did you get too near the bread knife again?

>

> Sabre

Erm, no; I've got a new kitchen restraining order so no knives for me.

(as if I care, duh) I made that mess accidentally, not on purpose so

it doesn't count actually ;-)

kind regards,

Jos

ps. it even wasn't even a big mess, just a little experiment.

JosAHa at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 16
ok..so can this be implemented by Sax..pls attached the sax code of getting the node values in the xml....as iam very new to this..
thinkmada at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 17
sax is also possible...pls provide me the sample code..
thinkmada at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 18
pls guys it's urgent..help me
thinkmada at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 19

> pls guys it's urgent..help me

I already told you how you could do it:

1) get a SAXParser

2) extend the DefaultHandler class

3) feed you handler to the parse() method of the SAXParser.

Whenever text is parsed (the text you want), the 'character' method

of your handler will be called by the SAXParser. Check the API docs.

kind regards,

Jos

JosAHa at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 20

can someone provide me the code idea for that xml...as iam very new to this...

same xml:

<?xml version="1.0" encoding="UTF-8"?>

<Main>

<AAAAA>

<ES>ESValue</ES>

<EI>EIValue</EI>

</AAAAA>

<BBBBB>

<SIP>

<ST>STValue</ST>

<TB>TBValue</TB>

<PM>PMValue</PM>

<VIP>

<CARP>

<AN1>AN1Value</AN1>

<BN>BNValue</BN>

</CARP>

<DARP>

<SA>

<AN2>AN2Value</AN2>

<CN>CNValue</CN>

</SA>

</DARP>

</VIP>

</SIP>

</BBBBB>

</Main>

thinkmada at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 21

> can someone provide me the code idea for that

> xml...as iam very new to this...

>

> same xml:

[ so no need to post it again: *snip* ]

Have you read the API docs for the classes I have mentioned? You can't

just sit and whine here asking other people to do your work for you.

Here's a bit of a giveaway; the following method is all you need in your

class extending the DefaultHandler:public void characters (char ch[], int start, int length) throws SAXException {

String s= new String(ch, start, length).trim();

// do something with string 's'

}

kind regards,

Jos

JosAHa at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 22

Hallo, you ca try this peice of Code.

It will be help out you.

import org.w3c.dom.Document;

import org.w3c.dom.Node;

public class TreeTraversal {

public static void main(String[] args) throws Exception

{

// First get a document builder factory

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

// Now get a document builder

DocumentBuilder db = null;

try {

db = dbf.newDocumentBuilder();

}

catch (ParserConfigurationException pce) {

pce.printStackTrace();

return;

}

// Call the parse method on DocumentBuilder. This returns an object

// of type Document

Document myDoc = db.parse(args[0]);

Now we just print the nodeType, nodeName and nodeValue of the

document node. Look at the JavaDoc for the Node interface and

you will see that for Document node:

name = "#document" (i.e., a fixed string)

value = null

process(myDoc, "\t");

}

Process all the child nodes of the Node object passed in. Processing means:

i) print out the nodeType, nodeName and nodeValue of a node

ii) call the process method with the node to process any children the node

has.

private static void process(Node node, String indent) {

Node c = null;

Go through each of the children one by one and call process. Note that

this code does a pre-order traversal of the tree.

You will also see that the parser considers new-lines to be Text content.

If you modify the file and remove new-line characters, you will see a

different output.

Use for loop to get out the data

for (c = node.getFirstChild(); c != null; c = c.getNextSibling()) {

printNode(c, indent);

process(c, indent + "\t");

}

}

To print the data in the console

private static void printNode(Node node, String indent){

System.out.println(indent

+ "node type = " + node.getNodeType()

+ ",\tname = " + node.getNodeName()

+ ",\tvalue = " + node.getNodeValue());

}

}

Best Regards

Maruthi

mars@sayampua at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 23
*sigh*Jos
JosAHa at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 24
> *sigh*> > JosI was wondering how long it would take for someone to give him teh codez and put this thread out of it's misery.
hunter9000a at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...
# 25

> > *sigh*

> >

> > Jos

>

> I was wondering how long it would take for someone to

> give him teh codez and put this thread out of it's misery.

Yep, another assignment ruined by copy/paste. Oh well, let's just hope

they fail.

kind regards,

Jos

JosAHa at 2007-7-21 20:20:07 > top of Java-index,Java Essentials,Java Programming...