0

I'm working with python xml.dom. I'm looking for a particular method that takes in a node and string and returns the xml node that is is named string. I can't find it in the documentation

I'm thinking it would work something like this

nodeObject =parent.FUNCTION('childtoFind')

where the nodeObject is under the parent

Or barring the existence of such a method, is there a way I can make the string a node object?

1
  • 1
    Any specific reason you cannot use the ElementTree API instead? Commented Feb 11, 2013 at 21:24

1 Answer 1

1

You are looking for the .getElementsByTagname() function:

nodeObjects = parent.getElementsByTagname('childtoFind')

It returns a list; if you need only one node, use indexing:

nodeObject = parent.getElementsByTagname('childtoFind')[0]

You really want to use the ElementTree API instead, it's easier to use. Even the minidom documentation makes this recommendation:

Users who are not already proficient with the DOM should consider using the xml.etree.ElementTree module for their XML processing instead.

The ElementTree API has a .find() function that let's you find the first matching descendant:

element = parent.find('childtoFind')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is helpful. I was hoping it wasn't in the documentation. I didn't see the ElementTreeAPI thing directly referenced in the documentation. I'll take a look at it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.