elementtree

'lxml.etree._ElementTree' object has no attribute 'insert'

'lxml.etree._ElementTree' object has no attribute 'insert' Question: I am trying to parse through my .xml file using glob and then use etree to add more code to my .xml. However, I keep getting an error when using doc insert that says object has no attribute insert. Does anyone know how I can effectively add code …

Total answers: 1

Python ElementTree XPath multiple conditions for attributes

Python ElementTree XPath multiple conditions for attributes Question: I’m trying to select nodes from my XML by the following rules: parent of node named "tag" with attributes k=’k1′ and (v=’v1′ or v=’v2′) How can I achieve it using python and ElementTree? I tried the following code, but got an error "SyntaxError: invalid predicate" roads = …

Total answers: 2

How to include xml prolog to xml files using python 3?

How to include xml prolog to xml files using python 3? Question: I want to include the XML prolog in my XML file… I tried the following – ET.tostring(root, encoding=’utf8′, method=’xml’) But it works only while printing and not for writing to file. I have a small code where I am changing an attribute and …

Total answers: 2

How to insert a processing instruction in XML file?

How to insert a processing instruction in XML file? Question: I want to add a xml-stylesheet processing instruction before the root element in my XML file using ElementTree (Python 3.8). You find as below my code that I used to create XML file import xml.etree.cElementTree as ET def Export_star_xml( self ): star_element = ET.Element(“STAR”,**{ ‘ …

Total answers: 1

Parsing XML with python ElementTree: ParseError: mismatched tag

Parsing XML with python ElementTree: ParseError: mismatched tag Question: I have several XML files I have to parse through with python ElemetTree (they are legacy from another developer). I’ve corrected those files a bit and parsed a good chunk so far but at some moment I got this parsing error, and I can’t get around …

Total answers: 3

Keeping CDATA sections while parsing through XML

Keeping CDATA sections while parsing through XML Question: I am trying to convert existing Xml file to another xml file with adding few nodes. But when i parse my original xml file and write it to the another xml file, it removes all the CDATA from the output xml. How can i avoid it ? …

Total answers: 2

What are the differences between lxml and ElementTree?

What are the differences between lxml and ElementTree? Question: When it comes to generating XML data in Python, there are two libraries I often see recommended: lxml and ElementTree From what I can tell, the two libraries are very similar to each other. They both seem to have similar module names, usage guidelines, and functionality. …

Total answers: 2

ElementTree better way to search out nodes (XPATH) using AND and 'parent'

ElementTree better way to search out nodes (XPATH) using AND and 'parent' Question: I need to find tag=ITEM that match 2 criteria, and then get the parent tag=NODE@name based on this find. Two issues: I can’t find a way for XPath to do an ‘and’, for example item = node.findall(‘./ITEM[@name=”toppas_type” and @value=”output file list”]’) Getting …

Total answers: 2

How to make a copy of xml tree in python using ElementTree?

How to make a copy of xml tree in python using ElementTree? Question: I’m using xml.etree.ElementTree to parse xml file. I’m parsing xml file in the following way: import xml.etree.ElementTree as ET tree = ET.parse(options.xmlfile) root = tree.getroot() This is my xml file: <rootElement> <member> <member_id>439854395435</member_id> </member> </rootElement> Then I’m saving it: tree.write(options.outcsvfile) How can …

Total answers: 2