Jump to content

XML to dictionary / tree structure

- - - - -

  • Please log in to reply
5 replies to this topic

#1
agnl666

agnl666

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly
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 : )

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

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.
Well, stuff like JDOM are trees, no?
You can do
Element root = doc.getRootElement();
List allChildren = root.getChildren();
getChild,
removeChild,
etc...

What exactly do you mean by a dictionary structure?

#3
agnl666

agnl666

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly
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.

#4
Smilex

Smilex

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
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.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
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:

<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/item

Would 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
agnl666

agnl666

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly
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