elementtree

Checking if XML declaration is present

别来无恙 提交于 2019-12-13 01:28:23
问题 I am trying to check whether an xml file contains the necessary xml declaration ("header"), let's say: <?xml version="1.0" encoding="UTF-8"?> ...rest of xml file... I am using xml ElementTree for reading and getting info out of the file, but it seems to load a file just fine even if it does not have the header. What I tried so far is this: import xml.etree.ElementTree as ET tree = ET.parse(someXmlFile) try: xmlFile = ET.tostring(tree.getroot(), encoding='utf8').decode('utf8') except: sys

Converting my python script from lxml to xml.etree

元气小坏坏 提交于 2019-12-13 00:39:01
问题 I am trying to convert my script (https://github.com/fletchermoore/n2c2) to use the default package xml.etree instead of lxml. This was an oversight on my part, but now I am realizing it would be impossible to get my target audience to set up lxml on their macs. I think that most of the code should just work by switching out the import, but when I tried it I found out that xml.etree handles namespaces differently (which I do not understand). Specifically, what would be the easiest way to

Replacing XML element in Python

ぃ、小莉子 提交于 2019-12-12 14:32:39
问题 I am trying to replace the element inside of bbox with a new set of coordinates. my code : # import element tree import xml.etree.ElementTree as ET #import xml file tree = ET.parse('C:/highway.xml') root = tree.getroot() #replace bounding box with new coordinates elem = tree.findall('bbox') elem.txt = '40.5,41.5,-12.0,-1.2' my xml file: <geoEtl> <source> <server>localhost</server> <port>xxxx</port> <db>vxxx</db> <user>xxxx</user> <passwd>xxxx</passwd> </source> <targetDir>/home/firstuser/<

Tags with : in name in lxml

这一生的挚爱 提交于 2019-12-12 10:56:32
问题 I'm trying to use lxml.etree to parse a Wordpress export document (it's XML, somewhat RSS like). I'm only interested in published posts, so I'm using the following to loop through published posts: for item in data.findall("item"): if item.find("wp:post_type").text != "post": continue if item.find("wp:status").text != "publish": continue write_post(item) where data is the tag that all item tags are found in. item tags contain posts, pages, and drafts. My problem is that lxml can't find tags

Does python's xml.etree.ElementTree support DTD?

北城余情 提交于 2019-12-12 10:14:11
问题 Does xml.etree.ElementTree support DTD? if it supports it, can I force ElementTree check a XML file against a dtd file, even if the XML file already has one. (internal or external). 回答1: I'm not sure about xml.etree , but lxml supports DTD validation: http://lxml.de/validation.html 来源: https://stackoverflow.com/questions/5396135/does-pythons-xml-etree-elementtree-support-dtd

lxml.etree and xml.etree.ElementTree adding namespaces without prefixes(ns0, ns1, etc.)

孤街醉人 提交于 2019-12-12 09:53:33
问题 There is any solution to add namespaces without prefix(i mean these ns0, ns1) which working on all the etree implementations or there are working solutions for each one? For now I have solutions for: lxml - nsmap argument of Element (c)ElementTree (python 2.6+) - register namespace method with empty string as a prefix The problem is (c)ElementTree in python 2.5, I know there is _namespace_map attribute but setting it to empty string creating invalid XML, setting it to None adding default ns0

Selecting element by ID, in .svg xml

 ̄綄美尐妖づ 提交于 2019-12-12 09:11:18
问题 I generate a .svg image (which is a xml file) using Inkscape. I set a node's ID to 'mount-arm-r'. I want to read the attributes 'x', 'y', from that element. I can't seem to select the rect elements. python: I tried XPath, or manually one depth at a time. I think I want: def parse_xml(): tree = ElementTree() tree.parse("torso-human.svg") for node in tree.findall('.//rect'): print 'n=', node xml: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- Created with Inkscape (http://www

How should I parse this xml string in python?

梦想的初衷 提交于 2019-12-12 08:01:27
问题 My XML string is - xmlData = """<SMSResponse xmlns="http://example.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <Cancelled>false</Cancelled> <MessageID>00000000-0000-0000-0000-000000000000</MessageID> <Queued>false</Queued> <SMSError>NoError</SMSError> <SMSIncomingMessages i:nil="true"/> <Sent>false</Sent> <SentDateTime>0001-01-01T00:00:00</SentDateTime> </SMSResponse>""" I am trying to parse and get the values of tags - Cancelled, MessageId, SMSError, etc. I am using python's

How to create a soap request using Python ElementTree, LXML or similar library

筅森魡賤 提交于 2019-12-12 06:57:40
问题 I am trying to create XML SOAP request using data from excel sheet. Currentlly, I have used mako templates but it requires XML template. How do I create a request with namespace like below (this is just a small sample not the complete XML): <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mes="http://www.orange.com/Webalc/Interfaces/ManageSupplierQuote/RequestForQuotation/v3/message"> <soapenv:Header/> <soapenv:Body> <mes:CalculateSupplierQuote> <!--1 to 500

Use Python Element Tree to parse xml in ASCII text file [closed]

五迷三道 提交于 2019-12-12 06:48:04
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I have ASCII text files that contain XML sections in them. I try the following basic commands to open the file, but get an error: import xml.etree.ElementTree as ET tree = ET.parse('data_file.txt') Is there a way I can still use Element Tree to be able to parse the XML sections out of the text file? 回答1: You