473,406 Members | 2,387 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,406 software developers and data experts.

DOM XML getAttributeByID

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.newInstan ce();
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)).getElementsBy TagName("*");

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

Jan 4 '06 #1
1 2156
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

Jan 4 '06 #2

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

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.