dom java xml getAttributeByID

R

rafiamar

hello,
if anyone could help me i'll be grateful
i have this xml:


<?xml version="1.0" encoding="UTF-8"?>
<appage_db>
<cycle id="Pete">
<machine>"t" </machine>
<passwd>"403" </passwd>
<user>"766" </user>
</cycle>
<cycle id="Pro">
<machine>"p" </machine>
<passwd>"411" </passwd>
<user>"766" </user>
</cycle>
</appage_db>


and i have this code:


class ProcessFruits {
public static void main (String[] args) {
try {
javax.xml.parsers.DocumentBuilderFactory factory =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder domBuilder =
factory.newDocumentBuilder();
org.w3c.dom.Document xmlDocument = domBuilder.parse(new
java.io.File("c:\\fff_config.xml"));
org.w3c.dom.NodeList Cycle =
xmlDocument.getElementsByTagName("cycle");
System.out.println("Found " + Cycle.getLength() + "
elements:");


for (int i = 0; i < Cycle.getLength(); i++) {


System.out.println("<Cycle> element " + (i + 1) + ":");
org.w3c.dom.NodeList Descendants =
((org.w3c.dom.Element)Cycle.item(i)).getElementsByTagName("*");


for (int j = 0; j < Descendants.getLength(); j++) {
org.w3c.dom.Node node = Descendants.item(j);
if (node.getChildNodes().getLength() == 1) {
System.out.println("element " + node.getNodeName() +
":" + node.getFirstChild().getNodeValue());
}
}


}
}
catch (Exception e) {
System.out.println(e);
}
} }


I need to get the elements only from the node which has the "id" i want

this program shows all elements.


thanks guys


Rafael Amar


Reply
 
S

Stefan Schulz

Ermmm... it might be just me, but i think you are getting what you
asked for, after all you ask for all children of tagname "cycle" and do
not mention any id anywhere.
 
R

Rafael Amar

Hi Stefan,
this program is showing all the children but i don't want all the
children just the children of the "cycle" which i'm sreaching for

like getAttributeByID("Pete);

and now it's showing all the children of all the id's.

Thanks
Rafael Amar
 
I

inexplicable

It seems that you're using JDOM, well, I think using JDOM alone
could achieve your requirement by filtering the children manually,
or you can have the alternative to using XPATH for help, and
jaxen could be an assistant project in this aspect.
 
A

Andrew Thompson

hello,
if anyone ..

Perhaps someone on one of the other groups to which you
multi-posted this problem, can help.

[ Please refrain from multi-posting. ]
 
A

Andrew Thompson

Rafael Amar wrote:

[multi-post]
I sorry i didn't know i couldn't ..

You have the technology to do so, but it it is not generally
productive, nor encouraged. People who do it regularly tend
to become ignored very quickly (though a single time, through
lack of knowledge, does not matter much).

If a message is relevant to two groups, it is better to cross-post.
[ Then to get extra 'cred'[1] you might also set follow-ups to the
single most connected group, and put a sentence in the post
mentioning which group you have set the follow-ups to. ]

[1] Crediblility or respect for politeness and forethought.

Unfortunately the advice given out by your web based interface to
usenet is not entirely clear, on this page
<http://groups.google.com/support/bin/answer.py?answer=12348&query=cross&topic=0&type=f>
it states..
"Few things annoy Usenet readers as much as multiple copies of
a posting appearing in multiple newsgroups (called "spamming").
A posting that is cross-posted (...good things about cross-posting)"

The thing they refer to as "spamming" has come to mean
"unsolicited advertising" (on any single group that
discourages or bans advertising), as opposed to posting
a message to a number of groups separately, that is
known as "multi-posting".
<http://www.google.com.au/search?hl=en&q=definition+"multi-posting"&btnG=Search&meta=>

Unfortunately, the general technique of spammers *is* to
multi-post, to as many groups as they can ley their grubby
little software on, so I can understand why the terms have
not yet gained a clear distinction.

[ Hope you solve the technical problem with getAttribute. ]
 
R

Rafael Amar

i found the answer.....

for (int i = 0; i < Cycle.getLength(); i++) {

org.w3c.dom.NamedNodeMap ggg =
((org.w3c.dom.Element)Cycle.item(i)).getAttributes();

String gggg = ggg.getNamedItem("ID").toString();
int index =gggg.indexOf("Pete");
if ( index == -1){
not found
}
else{found}

Thanks

Rafael Amar
 
S

Stefan Schulz

Did you use a validating parser? I am not sure since i usually do a
manual ID->Element mapping (too much uncertainty if you use a schema),
but IIRC as soon as you use a non-validating parser, all bets are off.
 
I

inexplicable

I think the following code could make sense.



Object jdomNode = ...; // Document, Element etc.
XPath path = new JDOMXPath("//cycle[@id]");

List results = path.selectNodes(jdomNode);

You can keep using JDOM, just plus Jaxen packages as well,
could help you to locate the elements you want by XPath.
 
C

Chris Smith

inexplicable said:
It seems that you're using JDOM, well, I think using JDOM alone
could achieve your requirement by filtering the children manually,
or you can have the alternative to using XPATH for help, and
jaxen could be an assistant project in this aspect.

There's no indication that anyone is using JDOM. In fact, the fact that
Rafael posted some code that doesn't use JDOM could be a good indication
that he's not, in fact, using JDOM.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top