Hi. I am working on a application that pulls down from a server (not my own) a string of xml. In ruby there are some pretty simple packages oriented to converting the string directly into a dictionary though I haven't found any in java. I have found quite a few SAX and DOM ways of constructing xml though I was wondering if anyone was aware of any library's for java that are useful in converting xml into a dictionary or tree like structure.
I have been working on a method of my own and I have a pretty good concept of how to do it though I'd rather not reinvent the wheel if I don't have too.
Thanks : )
5 replies to this topic
#1
Posted 23 March 2011 - 01:34 PM
|
|
|
#2
Posted 24 March 2011 - 05:47 AM
agnl666 said:
I have found quite a few SAX and DOM ways of constructing xml though I was wondering if anyone was aware of any library's for java that are useful in converting xml into a dictionary or tree like structure.
You can do
Element root = doc.getRootElement();
List allChildren = root.getChildren();
getChild,
removeChild,
etc...
What exactly do you mean by a dictionary structure?
#3
Posted 24 March 2011 - 01:57 PM
Well when I looked into DOM and SAX methods of dealing with xml I found that it was mainly used for building xml files.
By dictionary I mainly mean a tree like data structure though instead of searching from node to node I would like to be able to specify the search by the tag and then have it return the node or the value/list of values contained by the node. Dictionary was just another term I was introduced to describing the same thing.
I have to search through large xml strings so I could use regular expressions though I thought that it may be simpler to conceptualize and distribute the data the xml contained if I stored into a tree.
By dictionary I mainly mean a tree like data structure though instead of searching from node to node I would like to be able to specify the search by the tag and then have it return the node or the value/list of values contained by the node. Dictionary was just another term I was introduced to describing the same thing.
I have to search through large xml strings so I could use regular expressions though I thought that it may be simpler to conceptualize and distribute the data the xml contained if I stored into a tree.
#4
Posted 24 March 2011 - 06:04 PM
Java API for XML Processing - Wikipedia, the free encyclopedia <- this I found for Java
I've done this in javascript before, and if I remember correctly, the DOM had great xml reading functions.
I've done this in javascript before, and if I remember correctly, the DOM had great xml reading functions.
#5
Posted 24 March 2011 - 11:06 PM
Well, if it's for pure searching, i find xpath pretty powerful.. Every xml jar propably has a function for it.
Here's an example I used from another thread:
To search for names containing 'to', all I need to do is use this xpath expression, and it returns the items
Well plainly
The brackets [ ] Can actually be read as a WHERE condition.
So
In xpath a dot/point/'.' means current element. So because I select the name, i don't need to go deeper and check a child or attribute, but the name itself --> '.'
Here's an example I used from another thread:
<food> <item> <name>tomato</name> <color>red</color> </item> <item> <name>banana</name> <color>yellow</color> </item> <item> <name>toast</name> <color>yellow/brown</color> </item> </food>
To search for names containing 'to', all I need to do is use this xpath expression, and it returns the items
/food/item[contains(name,'to')]To get only the names:
/food/item/name[contains(.,'to')]
Well plainly
/food/itemWould result in a list of all 'item' nodes in 'food' right.
The brackets [ ] Can actually be read as a WHERE condition.
So
/food/item[contains(name,'to')]Would be: Select all 'item' from 'food' where the 'name' element of 'item' contains 'to'.
/food/item/name[contains(.,'to')]This only selects the names WHERE . contains 'to'.
In xpath a dot/point/'.' means current element. So because I select the name, i don't need to go deeper and check a child or attribute, but the name itself --> '.'
#6
Posted 27 March 2011 - 05:39 PM
Thank you very much. I intend to look through xpath and hopefully this will save me some time.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









