LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2012, 01:52 AM   #1
otkaz
LQ Newbie
 
Registered: Apr 2009
Location: Houston, TX
Posts: 26

Rep: Reputation: 0
python mini.dom question


Learning to program and I'm hitting some road blocks. Having trouble looking up an answer due to not knowing what the thing I'm looking for is called.
I'm using mini.dom to read an xml config file and I want to search for a tag name and write the "position id" to a variable. not sure what the position id is really called...
getElementsByTagName('tag')[0] <--- that 0 which means it was found first would be a 1 if found 2nd
What is the proper name of this and how could I do something like this
Code:
for element in node.getElementsByTagName('sometag'):
    if element.firstChild.data == 'tagimlookingfor':
        elementid = element.positioninthearray #<----how do I write the [0], or [1] to a variable so I can use it to discribe the position in the next line
        var = node.getElementsByTagName('sometag')[elementid].firstChild.data
and yes I know I could just var = node.getElementsByTagName('tagimlookingfor')[0].firstChild.data but is a little more complicated then that I need to figure out the position do to the structure of the xml I'm parsing
 
Old 11-24-2012, 09:27 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It would probably help if you gave an example of the xml (or at least a sample that would explain your problem).

Looking at the code you gave var = element.firstChild.data shoudl be sufficient but I'm prepared to accept that in a more complex scenario that might not answer your needs.

Another approach if you need the index, then add a counter outside of the loop, initialised to 0, and increment by 1 at the end of the loop.

Often it is possible to avoid such looping by using xpath (and the functions)
 
Old 11-24-2012, 10:17 PM   #3
otkaz
LQ Newbie
 
Registered: Apr 2009
Location: Houston, TX
Posts: 26

Original Poster
Rep: Reputation: 0
Index is what that number is called? I tried googling "mini.dom index" but didn't turn up with anything. So is there a function to grab the index for a search result?
Here is an example of how the xml might look.
Code:
<stuff>.
    <morestuff>
        <sometag>catagory1</sometag>
        <path pathversion="1">/path Im looking to for</path> #<--info i'm after
        <path pathversion="2">/path I don't need</path>
        <path pathversion="3">/path I don't need</path>
    </morestuff>
    <morestuff>
        <sometag>catagory2</sometag>
        <path pathversion="1">/other path I'm looking for</path> #<--info i'm after
        <path pathversion="2">/path I don't need</path>
        <path pathversion="3">/path I don't need</path>
    </morestuff>
</stuff>
Problem is category1 and catagory2 might not be in that order and I need to grab the first path listed for each and know which is which
 
Old 11-24-2012, 10:44 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
using xpath you can get the two nodes (using your example) as follows:
Code:
//path[@pathversion='1']
you can test this using an "xpath expression tester" many exist on the web. That will return the elements so you don't need to know their specific index within the document

and to get the category tag use this xpath
Code:
//path[@pathversion='1']//../sometag
the //.. gets the parent and then /sometag gets the soemtag element

Last edited by graemef; 11-24-2012 at 11:05 PM. Reason: getting the category
 
Old 11-24-2012, 10:56 PM   #5
otkaz
LQ Newbie
 
Registered: Apr 2009
Location: Houston, TX
Posts: 26

Original Poster
Rep: Reputation: 0
Great I'll play with this for a while. Thanks for pointing me in the right direction.

Last edited by otkaz; 11-24-2012 at 11:09 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Question on mini iso install dlinuxh Linux - Distributions 1 04-06-2012 03:39 AM
LXer: Avoiding Linux Installation Problems on the HP Mini 110 and Mini 210 Netbooks LXer Syndicated Linux News 0 06-13-2010 10:10 PM
java xml DOM question kpachopoulos Programming 2 12-16-2009 09:35 AM
mini itx s-video tv connection question Michael_aust Linux - Hardware 0 01-13-2007 06:13 PM
simple mini-PCI wireless question djzanni Linux - Hardware 7 12-16-2006 10:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration