File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Query Regarding XML DOM Parser use in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Query Regarding XML DOM Parser use in Java" Watch "Query Regarding XML DOM Parser use in Java" New topic
Author

Query Regarding XML DOM Parser use in Java

Azeem Mohammed
Greenhorn

Joined: Jan 15, 2009
Posts: 12
Hi,
I have an xml file which defines a table with certain columns.The columns differ primarily on their 'type' attribute.
For eg:

/*xml snippet starts*/
<Column name="CA" type="STRING" xax="true" index="0" />
<Column name="BA" type="STRING" xax="true" index="1" />
<Column name="DA" type="DOUBLE" index="2" />
//Each Row is associated with cells corresponding to the columns as shown below:-
<Row>
<Cell>Tees</Cell>
<Cell>Bangladesh</Cell>
<Cell>2603.299</Cell>
/*xml snippet ends */


I am trying to write a java program which does something like this in pseudo code:-

if(cell.isOfTypeString()){
doSomething();
}
else{
doSomethingElse();
}

I am not able to figure out that for a given object "cell",how to figure out if it's type attribute is equal to "STRING" .

Any help will be greatly appreciated.

Thanks.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Read the "type" attribute's value comes into mind.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Azeem Mohammed
Greenhorn

Joined: Jan 15, 2009
Posts: 12
I tried doing that but my program is not working properly.This might sound stupid,but Is it necessary that a column attribute be associated with a cell in that column?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

I see two options:

1) You need to store the types with the column as it is now, then when you encounter a cell find its matching column (based on index?) and use the column's type.
2) Give each Cell element its own type attribute.
Azeem Mohammed
Greenhorn

Joined: Jan 15, 2009
Posts: 12
I can't modify the xml.It's something that I have to take as an input.So, option 1 is gone.
The second option sounds good, but can you suggest any DOM Library function to get the Column,given a cell ?
Thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Apparently the order of the Column elements matches the order of the Cell elements, right?

I would get the NodeList for the Column elements using getElementsByTagName("Column") plus
the NodeList for the Cell elements ... ditto

Now, since NodeList maintains the order of elements in the document, you can iterate through either list and get the matching element from the other list - see the NodeList.item() method.

All documented in the standard Java library for org.w3c.dom package - pay close attention to the table in the Node interface javadocs.

Bill

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Azeem Mohammed wrote:I can't modify the xml.It's something that I have to take as an input.So, option 1 is gone.
The second option sounds good, but can you suggest any DOM Library function to get the Column,given a cell ?
Thanks.

You got it wrong. Option 2 requires you to modify the XML so that option's out of the question. So you need to map a Cell to a Column. I already suggested using the index. When you encounter a Row element the index gets reset to 0. After each Cell element you increment it by one. That way, your Cell elements are "numbered" 0, 1, 2, ...
You store the Columns in a List in the order they are encountered. The List will give them indexes. So now you have an index for a Cell and an index for a Column. It should be quite easy to match the two now.
Azeem Mohammed
Greenhorn

Joined: Jan 15, 2009
Posts: 12
Thanks a lot Rob & William,for your help.I have got it working now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query Regarding XML DOM Parser use in Java