PythonXMLMinidom。生成<;文本>;一些文本</文本>;要素
我有以下代码PythonXMLMinidom。生成<;文本>;一些文本</文本>;要素,python,xml,minidom,Python,Xml,Minidom,我有以下代码 from xml.dom.minidom import Document doc = Document() root = doc.createElement('root') doc.appendChild(root) main = doc.createElement('Text') root.appendChild(main) text = doc.createTextNode('Some text here') main.appendChild(text) print d
from xml.dom.minidom import Document
doc = Document()
root = doc.createElement('root')
doc.appendChild(root)
main = doc.createElement('Text')
root.appendChild(main)
text = doc.createTextNode('Some text here')
main.appendChild(text)
print doc.toprettyxml(indent='\t')
结果是:
<?xml version="1.0" ?>
<root>
<Text>
Some text here
</Text>
</root>
这里有一些文字
这一切都很好,但是如果我想让输出看起来像这样呢
<?xml version="1.0" ?>
<root>
<Text>Some text here</Text>
</root>
这里有一些文字
这容易做到吗
奥扬普
这容易做到吗
这取决于你想要什么样的确切规则,但通常不会,你对漂亮的印刷几乎没有控制权。如果你想要一种特定的格式,你通常必须自己写一个walker
DOM Level 3 LS参数格式pretty print in与您的示例非常接近。它的规则是,如果一个元素只包含一个TextNode,则不会添加额外的空白。但是,它(目前)使用两个空格作为缩进,而不是四个空格
>>> doc= pxdom.parseString('<a><b>c</b></a>')
>>> doc.domConfig.setParameter('format-pretty-print', True)
>>> print doc.pxdomContent
<?xml version="1.0" encoding="utf-16"?>
<a>
<b>c</b>
</a>
所有关于monkey patching不好的常见警告都适用。pyxml包通过使用xml.dom.ext.PrettyPrint()函数提供了一个简单的解决方案。它还可以打印到文件描述符 但是不再维护pyxml包
欧尔詹·佩特森(Oerjan Pettersen)我在寻找完全相同的东西,我看到了这篇文章。(xml.dom.minidom中提供的缩进破坏了我用来操作xml的另一个工具,我需要它缩进)我用一个稍微复杂一点的示例尝试了公认的解决方案,结果如下:
In [1]: import pxdom
In [2]: xml = '<a><b>fda</b><c><b>fdsa</b></c></a>'
In [3]: doc = pxdom.parseString(xml)
In [4]: doc.domConfig.setParameter('format-pretty-print', True)
In [5]: print doc.pxdomContent
<?xml version="1.0" encoding="utf-16"?>
<a>
<b>fda</b><c>
<b>fdsa</b>
</c>
</a>
-w开关会导致文件被覆盖,但令人烦恼的是,会留下一个名为“myfile.xml~”的文件,您可能会想删除它。其他的开关在那里是为了得到正确的格式(对我来说)
p.S.xmlindent是一种流格式化程序,即您可以按如下方式使用它:
cat myfile.xml | xmlindent > myfile_indented.xml
因此,如果需要的话,您可以在python脚本中使用它,而无需写入文件。此解决方案对我来说很有效,无需进行修补或停止使用minidom:
from xml.dom.ext import PrettyPrint
from StringIO import StringIO
def toprettyxml_fixed (node, encoding='utf-8'):
tmpStream = StringIO()
PrettyPrint(node, stream=tmpStream, encoding=encoding)
return tmpStream.getvalue()
这可以通过toxml()来完成,使用正则表达式来整理内容
>>> from xml.dom.minidom import Document
>>> import re
>>> doc = Document()
>>> root = doc.createElement('root')
>>> _ = doc.appendChild(root)
>>> main = doc.createElement('Text')
>>> _ = root.appendChild(main)
>>> text = doc.createTextNode('Some text here')
>>> _ = main.appendChild(text)
>>> out = doc.toxml()
>>> niceOut = re.sub(r'><', r'>\n<', re.sub(r'(<\/.*?>)', r'\1\n', out))
>>> print niceOut
<?xml version="1.0" ?>
<root>
<Text>Some text here</Text>
</root>
>来自xml.dom.minidom导入文档
>>>进口稀土
>>>doc=文件()
>>>root=doc.createElement('root')
>>>_ux=doc.appendChild(根目录)
>>>main=doc.createElement('Text')
>>>_uz=root.appendChild(主)
>>>text=doc.createTextNode('此处有一些文本')
>>>@main.appendChild(文本)
>>>out=doc.toxml()
>>>niceOut=re.sub(r'>\n最简单的方法是使用prettyxml,并删除标记中的\n和\t。这样您就可以按照示例中的要求保留缩进
xml\u output=doc.toprettyxml()
nojunkintags=re.sub('>(\n |\t)谢谢。因为我不会经常手动读取这些文件,而且这不容易做到,我想我还是保持原样吧。然后把我的注意力集中在更重要的事情上,比如让这个加载/保存到xml工作。Orjanp…是的,这里已经提到了这个解决方案。但是如果我没有弄错的话,这个解决方案需要使用pyxmlinstalled.Pyxml目前似乎没有维护。所以我想另一个解决方案是更好的,比如lxml。我也很想知道这一点,但我对公认的答案不满意,因为要使用它,我需要安装pxdom…使用当前版本的xml.dom.minidom,这个错误现在已经修复了。这个问题为我节省了几个小时
from xml.dom.ext import PrettyPrint
from StringIO import StringIO
def toprettyxml_fixed (node, encoding='utf-8'):
tmpStream = StringIO()
PrettyPrint(node, stream=tmpStream, encoding=encoding)
return tmpStream.getvalue()
>>> from xml.dom.minidom import Document
>>> import re
>>> doc = Document()
>>> root = doc.createElement('root')
>>> _ = doc.appendChild(root)
>>> main = doc.createElement('Text')
>>> _ = root.appendChild(main)
>>> text = doc.createTextNode('Some text here')
>>> _ = main.appendChild(text)
>>> out = doc.toxml()
>>> niceOut = re.sub(r'><', r'>\n<', re.sub(r'(<\/.*?>)', r'\1\n', out))
>>> print niceOut
<?xml version="1.0" ?>
<root>
<Text>Some text here</Text>
</root>