Java XML Parsing Errors

V

vunet.us

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
String e2val = e2.getAttribute("Type");

if(e1val.equals("Red") && e2val.equals("Sedan")){
newXML = //HOW TO ADD THIS NODE AS STRING?
}
return newXML;

}

THANK YOU!!!
 
M

Mark Jeffcoat

(e-mail address removed) 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.
 
V

vunet.us

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).
 
V

vunet.us

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;

}
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top