DOM XML getAttributeByID

R

Rafael Amar

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
 
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
 

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