Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Java XML Parsing Errors

Thread view: 
vunet.us@gmail.com - 18 Oct 2006 21:36 GMT
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!!!
Mark Jeffcoat - 18 Oct 2006 23:35 GMT
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).
vunet.us@gmail.com - 19 Oct 2006 14:42 GMT
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;

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.