G
G
Grigory Kalyashov2014-07-16 11:37:47
Django
Grigory Kalyashov, 2014-07-16 11:37:47

How to get an element from the DOM tree?

<page>
      <domains>
              <domain>
                     <emails>
                         <action-status/>
                                       <email>
                                             <name>[email protected]</name>
                                       </email>
                                        <email>
                                              <name>[email protected]</name>
                                       </email>
                                       <email>
                                                <name>[email protected]</name>
                                        </email>
                                         <email>
                                                 <name>[email protected]</name>
                                        </email>
                                        <email>
                                                 <name>[email protected]</name>
                                       </email>
...

How to get and list the elements in the tag?
tried like this:
DOMTree = xml.dom.minidom.parse(urllib.urlopen(path))
    collection = DOMTree.documentElement
    movies = collection.getElementsByTagName("email")
  
    data = []
    for movie in movies:
       type = movie.getElementsByTagName('name')[0]
       data.append(type.childNodes[0].data) 

      return HttpResponse(data)

But outputs everything in a line.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Borisovich, 2014-07-16
@kalyashov

But outputs everything in a line.

Well, do you have data.append and add it to the end every time? And how should it be, on a new line or do you want to collect them into an object?
I think xpath requests are more convenient
https://developer.mozilla.org/en-US/docs/Web/API/D...
var headings = document.evaluate("*//email/name", document, null, XPathResult.ANY_TYPE, null); 
var thisHeading = headings.iterateNext(); 
var alertText = "Level 2 headings in this document are:\n";
while (thisHeading) {
  alertText += thisHeading.textContent + "\n";
  thisHeading = headings.iterateNext();
}
alert(alertText); // Alerts the text of all h2 elements

But you have some kind of architectural problem. How do you do something weird?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question