minidom

All nodeValue fields are None when parsing XML

有些话、适合烂在心里 提交于 2019-12-03 13:37:06
I'm building a simple web-based RSS reader in Python, but I'm having trouble parsing the XML. I started out by trying some stuff in the Python command line. >>> from xml.dom import minidom >>> import urllib2 >>> url ='http://www.digg.com/rss/index.xml' >>> xmldoc = minidom.parse(urllib2.urlopen(url)) >>> channelnode = xmldoc.getElementsByTagName("channel") >>> channelnode = xmldoc.getElementsByTagName("channel") >>> titlenode = channelnode[0].getElementsByTagName("title") >>> print titlenode[0] <DOM Element: title at 0xb37440> >>> print titlenode[0].nodeValue None I played around with this for

Modify XML declaration with python

☆樱花仙子☆ 提交于 2019-12-02 21:00:50
问题 I have an XML document for which I need to add a couple of things to the XML declaration using minidom. The declaration looks like this: <?xml version="1.0"?> And I need it to look like this: <?xml version="1.0" encoding="UTF-16" standalone="no"?> I know how to change or add attributes using minidom, which will not work here. What is the easiest way of doing this? For reference, I am running python 3.3.3. 回答1: I'm not sure if this can be done with minidom. But you could try lxml . from lxml

Print all xml child node using python

牧云@^-^@ 提交于 2019-12-02 10:09:41
问题 I want to print all the values of the "ClCompiler" child of "ItemGroup" of my xml file. my python code tree = minidom.parse(project_path) itemgroup = tree.getElementsByTagName('ItemGroup') print (itemgroup[0].toxml()) my result <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> <Configuration>Debug</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Release|Win32"> <Configuration>Release</Configuration>

Preserving XML attribute order?

本秂侑毒 提交于 2019-12-02 07:21:03
问题 I know this question has been asked in the past, but they have all been dated a few years back. I am wondering if there has been any changes made to Python modules such as lxml, minidom, or etree that will allow us to preserve the attribute order in XML files without patching. I need the order to be preserved as the program I am supplying the files to relies on it. If there are no updates, what's the easiest way to implement this? 回答1: The insignificance of attribute ordering is not a

Print all xml child node using python

一笑奈何 提交于 2019-12-02 03:49:35
I want to print all the values of the "ClCompiler" child of "ItemGroup" of my xml file. my python code tree = minidom.parse(project_path) itemgroup = tree.getElementsByTagName('ItemGroup') print (itemgroup[0].toxml()) my result <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> <Configuration>Debug</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Release|Win32"> <Configuration>Release</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> </ItemGroup> <ItemGroup> <ClCompile Include="../../avmedia

Extracting text from XML node with minidom

本小妞迷上赌 提交于 2019-12-02 00:19:17
问题 I've looked through several posts but I haven't quite found any answers that have solved my problem. Sample XML = <TextWithNodes> <Node id="0"/>TEXT1<Node id="19"/>TEXT2 <Node id="20"/>TEXT3<Node id="212"/> </TextWithNodes> So I understand that usually if I had extracted TextWithNodes as a NodeList I would do something like nodeList = TextWithNodes[0].getElementsByTagName('Node') for a in nodeList: node = a.nodeValue print node All I get is None . I've read that you must write a.childNodes

Python Minidom XML Query

ぐ巨炮叔叔 提交于 2019-12-01 21:21:49
问题 I'm trying to query this XML with lxml: <lista_tareas> <tarea id="1" realizzato="False" data_limite="12/10/2012" priorita="1"> <description>XML TEST</description> </tarea> <tarea id="2" realizzato="False" data_limite="12/10/2012" priorita="1"> <description>XML TEST2</description> </tarea> I wrote this code: from lxml import etree doc = etree.parse(file_path) root = etree.Element("lista_tareas") for x in root: z = x.Element("tarea") for y in z: element_text = y.Element("description").text

Extracting text from XML node with minidom

浪子不回头ぞ 提交于 2019-12-01 20:16:31
I've looked through several posts but I haven't quite found any answers that have solved my problem. Sample XML = <TextWithNodes> <Node id="0"/>TEXT1<Node id="19"/>TEXT2 <Node id="20"/>TEXT3<Node id="212"/> </TextWithNodes> So I understand that usually if I had extracted TextWithNodes as a NodeList I would do something like nodeList = TextWithNodes[0].getElementsByTagName('Node') for a in nodeList: node = a.nodeValue print node All I get is None . I've read that you must write a.childNodes.nodeValue but there isn't a child node to the node list since it looks like all the Node Ids are closing

memory leak parsing xml using xml.dom.minidom

我们两清 提交于 2019-12-01 11:06:09
问题 I'm using xml.dom.minidom to parse xml files, somewhat like this: import xml.dom.minidom as dom file= open('file.xml') doc= dom.parse(file) # SNIP doc.unlink() Even after unlinking the document, the memory usage is at about 120 MiB. When one is actually using the program, causing multiple xml files to be parsed, memory usage climbs to about 300 MiB, which is unacceptable. I'm sure the memory leak isn't caused by my code, but by minidom, because even doing just doc= dom.parse(file) doc.unlink(

How to add an xml-stylesheet processing instruction node with Python 2.6 and minidom?

天涯浪子 提交于 2019-12-01 06:15:40
I'm creating an XML document using minidom - how do I ensure my resultant XML document contains a stylesheet reference like this: <?xml-stylesheet type="text/xsl" href="mystyle.xslt"?> Thanks ! Use something like this: from xml.dom import minidom xml = """ <root> <x>text</x> </root>""" dom = minidom.parseString(xml) pi = dom.createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="mystyle.xslt"') root = dom.firstChild dom.insertBefore(pi, root) print dom.toprettyxml() => <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="mystyle.xslt"?> <root> <x> text </x> </root> I am