File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Dom Parser picking up only first tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Dom Parser picking up only first tag" Watch "Dom Parser picking up only first tag" New topic
Author

Dom Parser picking up only first tag

Lou Pellegrini
Ranch Hand

Joined: Nov 11, 2003
Posts: 142
    
    1
Hi,

My Dom parser is picking up only the first tag, in this case <gram>. Can someone tell me why <carat> is not included?

Code and console output below.

Thanks,

Lou

XML file MKSFactors2.xml



DomParser.java


Console Output
Starting MeasurementList
DomParser.parseXmlFile()
dom=[#document: null]
parseDocument()
getMeasurement(Element element)
Measurement Added
Number of Measurements = 1.
Measurement label=Weight, type=Metric, name=Gram, toMKS=1.0, fromMKS=1.0

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
I was going to try running it, but the Measurement class is missing. Please post an SSCCE (<-- that's a link), or post the missing class(es).


Ping & DNS - updated with new look and Ping home screen widget
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12681
    
    5
textVal = el.getFirstChild().getNodeValue();


I always suspect that getFirstChild() returns some formatting crlf or spaces, and not the Node you are expecting.

Bill

Java Resources at www.wbrogden.com
Lou Pellegrini
Ranch Hand

Joined: Nov 11, 2003
Posts: 142
    
    1
Hi Ulf,

Sorry I didn't post all the classes.

All files follow my signature.

MeasurementList is the main class.

MKSFactors2.xml is a small file for testing and development. I have a much larger file for production after I get the smaller one working.

Thanks,

Lou

MeasurementList.java


Measurement.java


DomParser.java


MKSFactors2.xml
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
After a quick look at the code, I don't see how it could be adding more measurements to the list than a single one. In the parseDocument method it adds one measurement for each "weights" tag - of which there is only one. I think you need to iterate through all the children of the weights node, and add them as appropriate.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dom Parser picking up only first tag
 
Similar Threads
Reading NULL xml
element name and cdata
DOM Parsing-Very Very Basic Doubt
XML parsing with dom
Dom parser for XML file