• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Query Regarding XML DOM Parser use in Java

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the "type" attribute's value comes into mind.
 
Azeem Mohammed
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Rob & William,for your help.I have got it working now.
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic