473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

XML Tree Discovery (script, tool, __?)

Hi all,

Finally diving into XML programmatically. Does anyone have a best
practice recommendation for programmatically discovering the structure
of an arbitrary XML document via Python?

It seems like it is a common wheel I'd be re-inventing.

Thanks and cheers
EP

Oct 24 '05 #1
14 2221
er***********@gmail.com wrote:
Finally diving into XML programmatically. Does anyone have a best
practice recommendation for programmatically discovering the structure
of an arbitrary XML document via Python?

It seems like it is a common wheel I'd be re-inventing.


It is. Look up XML DOM.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
An ounce of hypocrisy is worth a pound of ambition.
-- Michael Korda
Oct 24 '05 #2
"inally diving into XML programmatically. Does anyone have a best
practice recommendation for programmatically discovering the structure
of an arbitrary XML document via Python?"

You can do this with DOM or SAX, or any of the many more friendly XML
processing libraries out there. You might want to be more specific.
What sort of output do you want from this discovery?

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

Oct 24 '05 #3
The output I was contemplating was a DOM "DNA" - that is the DOM
without the instances of the elements or their data, a bare tree, a
prototype tree based on what is in the document (rather than what is
legal to include in the document).

Just enough data that for an arbitrary element I would know:

1) whether the element was in a document
2) where to find it (the chain of parents)
As I mentioned, I'm just starting to think about the subject, so maybe
the best practice is something else like loading the full DOM into
memory. I'm still in the make one to throw away one mode.

EP

Oct 24 '05 #4
er***********@gmail.com wrote:
The output I was contemplating was a DOM "DNA" - that is the DOM
without the instances of the elements or their data, a bare tree, a
prototype tree based on what is in the document (rather than what is
legal to include in the document).

Just enough data that for an arbitrary element I would know:

1) whether the element was in a document
2) where to find it (the chain of parents)


how to you identify the elements ? do they have unique tags, unique identifiers,
or some other "globally unique" identifier (attributes or contents) ?

</F>

Oct 25 '05 #5
just namespace + tag

Oct 25 '05 #6
er***********@gmail.com wrote:
just namespace + tag


here's an ElementTree-based example:

# http://effbot.org/tag/elementtree
import elementtree.ElementTree as ET

FILE = "example.xml"

path = ()
path_map = {}

for event, elem in ET.iterparse(FILE, events=("start", "end")):
if event == "start":
path = path + (elem.tag,)
else:
path_map.setdefault(path, []).append(elem)
path = path[:-1]
elem.clear() # won't need the contents any more

for path in path_map:
print "/".join(path), len(path_map[path])

given this document:

<document>
<chapter>
<title>chapter 1</title>
</chapter>
<chapter>
<title>chapter 2</title>
</chapter>
</document>

the above script prints

document 1
document/chapter 2
document/chapter/title 2

the script uses universal names for tags that live in a namespace. for
information on how to decipher such names, see:

http://www.effbot.org/zone/element.htm#xml-namespaces

hope this helps!

</F>

Oct 26 '05 #7
"""
The output I was contemplating was a DOM "DNA" - that is the DOM
without the instances of the elements or their data, a bare tree, a
prototype tree based on what is in the document (rather than what is
legal to include in the document).

Just enough data that for an arbitrary element I would know:

1) whether the element was in a document
2) where to find it (the chain of parents)
"""

This is easy to do in SAX. For some hints, see page 2 of my article:

http://www.xml.com/pub/a/2004/11/24/py-xml.html

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

Oct 26 '05 #8
All I can add to this is:

- don't use SAX unless your document is huge
- don't use DOM unless someone is putting a gun to your head

There's a good selection of nice and simple XML processing libraries in
python. You could start with ElementTree.

Oct 26 '05 #9
Istvan Albert wrote:
All I can add to this is:

- don't use SAX unless your document is huge
- don't use DOM unless someone is putting a gun to your head


+1 QOTW
Oct 26 '05 #10
> - don't use SAX unless your document is huge
- don't use DOM unless someone is putting a gun to your head


What I say is: use what works for you. I think SAX would be fine for
this task, but, hey, I personally would use Amara (
http://uche.ogbuji.net/tech/4suite/amara/ ), of course. The following
does the trick:

import sets
import amara
from amara import binderytools

#element_skeleton_rule suppresses char data from the resulting binding
#tree. If you have a large document and only care about element/attr
#structure and not text, this saves a lot of memory
rules = [binderytools.element_skeleton_rule()]
#XML can be a file path, URI, string, or even an open-file-like object
doc = amara.parse(XML, rules=rules)
elems = {}
for e in doc.xml_xpath('//*'):
paths = elems.setdefault((e.namespaceURI, e.localName), sets.Set())
path = u'/'.join([n.nodeName for n in e.xml_xpath(u'ancestor::*')])
paths.add(u'/' + path)

#Pretty-print output
for name in elems:
print name, '\n\t\t\t', '\n\t\t\t'.join(elems[name])
--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

Oct 28 '05 #11
"er***********@gmail.com" wrote:
Hi all,

Finally diving into XML programmatically. Does anyone have a best
practice recommendation for programmatically discovering the structure
of an arbitrary XML document via Python?

It seems like it is a common wheel I'd be re-inventing.

Thanks and cheers

I was looking for something similar (XML to DTD inference) but I didn't
find anything related in python. Trang
(http://www.thaiopensource.com/relaxn...#introduction),
on the other hand seems impressive after a few non-trivial tests. It
would be neat to have it ported in python, at least the inference part.

George

Oct 28 '05 #12
"""
I was looking for something similar (XML to DTD inference) but I didn't
find anything related in python. Trang
(http://www.thaiopensource.com/relaxn...#introduction),
on the other hand seems impressive after a few non-trivial tests. It
would be neat to have it ported in python, at least the inference part.

"""

If you're OK with RELAX NG rather than DTD as the schema output
(probably a good idea if you're using namespaces), consider
Examplotron, which I've used on many such production tasks.

http://www-128.ibm.com/developerwork...ary/x-xmptron/

It's XSLT rather than Python, but the good news is that XSLT is easy to
invoke from Python using tools such as 4Suite.

http://uche.ogbuji.net/tech/akara/no...01/python-xslt

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

Oct 28 '05 #13
<uc*********@gmail.com> wrote:
"""
I was looking for something similar (XML to DTD inference) but I didn't
find anything related in python. Trang
(http://www.thaiopensource.com/relaxn...#introduction),
on the other hand seems impressive after a few non-trivial tests. It
would be neat to have it ported in python, at least the inference part.

"""

If you're OK with RELAX NG rather than DTD as the schema output
(probably a good idea if you're using namespaces), consider
Examplotron, which I've used on many such production tasks.

http://www-128.ibm.com/developerwork...ary/x-xmptron/

It's XSLT rather than Python, but the good news is that XSLT is easy to
invoke from Python using tools such as 4Suite.

http://uche.ogbuji.net/tech/akara/no...01/python-xslt


Neat, though non-trivial XSLT makes my head spin. Just for kicks, I
rewrote in python Michael Kay's DTDGenerator
(http://saxon.sourceforge.net/dtdgen.html), though as the original it
has several limitations on the accuracy of the inferred DTD.

George

Oct 29 '05 #14
"Neat, though non-trivial XSLT makes my head spin."

Well, you don't have to know XSLT at all to use the Examplotron
transform, although I can understand wanting to understand and hack
what you're using.

"Just for kicks, I
rewrote in python Michael Kay's DTDGenerator
(http://saxon.sourceforge.net/dtdgen.html), though as the original it
has several limitations on the accuracy of the inferred DTD. "

Ah. Cool. Got a link?

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

Oct 30 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tree menu using XML | last post by:
I have one XML file that has nodes and sub node and each and every node has the attribute call visible if its value is true then diplay this node else don't display thid node, but this condition i...
0
by: Daniel Thune, MCSE | last post by:
I am trying to use the WSDL.exe tool to create a proxy class for a webservice. I have the URL to the WSDL file for the service, but when I run the WSDL.exe tool and give it this URL I get the...
0
by: serge calderara | last post by:
Dear all, Let say that I have developped a web service which is host on Server1. Then one of my customer will like to used that service in its own web site which is not based on .NET platform. ...
1
by: Satish.Talyan | last post by:
hi, i want to create a dynamic tree hierarchy in javascript.there are two parts in tree, group & user.when we click on group then users come under that's tree category will be opened.problem is...
5
by: Ham | last post by:
Hi I am looking for a function call tree program to map the function calls of my project. I however do not want to compile my project. All the programs I have found for this require you to...
7
by: Chris Mullins | last post by:
I'm in the process of building a number of (Web) Services using .NET 3.0 and WCF. These services are intended to be deployed within the Intranet of a very, very large orginization. I need to...
1
by: Zghuol | last post by:
Hi..everybody..:) My problem is in abreviation..: the server was unable to register the administration ttool discovery information. The administration tool may not be able to see this server. The...
0
by: news.microsoft.com | last post by:
Hey All, Sorry to post to several groups but wasn't sure which development group would be best for the question. I was wondering if there are any developers out there that have significant...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.