ElementTree parsing XML (minidom parsing XML large file, Memoryerror)

Source: Internet
Author: User

When parsing an XML file using Minido, the result is an error memoryerror because the file is too large. After the query is learned because minidom in the parsing is to put all the files in memory, very memory-intensive, so consider a different way to process the XML file. ElementTreeCompared to minidom consumes less memory, the following is ElementTreeSome simple usages of the XML source file are part of the content:#导入ElementTreeFrom Xml.etree import ElementTree#读入并解析XML文件, the tree structure is read inDoc = et.parse (XML file address)#.getroot () Get the root node of the XML fileroot = Self.doc.getroot ()#.findall () finds all child nodes under the root nodeHttpsample_nodes = Root.findall (' httpsample ') #httpSample为子节点的tag
For I in Httpsample_nodes:
     Print ("The result of printing I is:", i)
          #.attrib all property results for the node are present in the dictionary structure
     Print ("The result of printing I.attrib is:", I.attrib)
#.attrib["RC"] get the attribute value of the node "RC"
     Print (' Print i.attrib[' RC "] results are: ', i.attrib[" RC "])
# . GetChildren () Gets all the child nodes of the node,. GetChildren () [0] Gets the first child node of a child node
     I.getchildren () [0]
     #.text getting the contents of a node
     Print ("The result of printing the text of I is:", I.text)
     Print ("I first child node content is:", I.getchildren () [0].text)
The specific print results are as follows:

ElementTree parsing XML (minidom parsing XML large file, Memoryerror)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.