Are you committed to org.w3c.dom? I personally much
prefer JDOM's interface (jdom.org), and I've used it
in two big projects now with no trouble at all. That
said, ...
> Hello,
> I am new to Java. I need to find two values in XML file, compare them
[quoted text clipped - 9 lines]
> String e1val = e1.getNodeValue("Color"); //ERROR: method getNodeValue
> not found in org.w3c.dom.Element
Yeah. That's because getNodeValue(String) isn't in the interface.
Odd that this wouldn't be an attribute too (though maybe
a car can have more than one color), but you probably want
to getElementsByTagName("Color"), find the sub-Element you
want, and ask for it's textContent.
If color's an attribute, you demonstrate how to do that
just below.
> String e2val = e2.getAttribute("Type");
>
[quoted text clipped - 4 lines]
>
> }
I have no idea what you're trying to accomplish in this part.
If I had this problem, I'd break this down into a series of
JUnit tests. First, establish that I know how to build an Element.
Next, that I can extract data from it. Next, that I could build
two Elements and compare the interesting values. Finally,
that I can "add to new XML String" ... whatever that happens
to mean.
Then, if I couldn't figure out how to get one of my tests
to pass, I might post the testcase here.

Signature
Mark Jeffcoat
Austin, TX
vunet.us@gmail.com - 19 Oct 2006 14:34 GMT
Thank you Mark,
All I need is find all elements in XML such as "Car" above, loop thru
them, find 2 other XML element values inside of a "Car" (such as
<Color>red</Color> and <Weight>12345</Weight>), compare values and if
condition met, add this Car to a new XML, which I need to be the string
in the end (at returning point). Please, note, that Car, Color and
Weight tags are made up by me for demonstration. That's all. I just
cannot figure out how to get values of the nodes and add the whole XML
piece to a new XML as a string (XML chunking).
Something is wrong with this test case. I will test it and let you know
soon. Thank you.
String new XML = null;
NodeList nodes = document.getElementsByTagName("Car");
for(int i=0; i<nodes.getLength(); i++){
Element e = (Element) nodes.item(i);
Element e = (Element) nodes.item(i);
NodeList Color = e.getElementsByTagName("Color");
NodeList Weight = e.getElementsByTagName("Weight");
String color = Color.getFirstChild().getNodeValue();
String weight = Weight.getFirstChild().getNodeValue();
if(color.equals("red") && weight.equals("12345")){
newXML = nodes.item(i).toString(); //HOW TO ADD THIS NODE AS
STRING?
}
return newXML;
}