Re: Java XML Parsing Errors
vunet.us@gmail.com writes:
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
with other two values and, if match, add to new XML string. Please,
help me with 2 errors:
String new XML = null;
NodeList nodes = document.getElementsByTagName("Car");
for(int i=0; i<nodes.getLength(); i++){
Element e1 = (Element) nodes.item(i);
Element e2 = (Element) nodes.item(i);
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");
if(e1val.equals("Red") && e2val.equals("Sedan")){
newXML = //HOW TO ADD THIS NODE AS STRING?
}
return newXML;
}
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.
--
Mark Jeffcoat
Austin, TX