LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   python mini.dom question (https://www.linuxquestions.org/questions/programming-9/python-mini-dom-question-4175438519/)

otkaz 11-24-2012 01:52 AM

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

graemef 11-24-2012 09:27 AM

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)

otkaz 11-24-2012 10:17 PM

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

graemef 11-24-2012 10:44 PM

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

otkaz 11-24-2012 10:56 PM

Great I'll play with this for a while. Thanks for pointing me in the right direction.


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