简体   繁体   English

在Python minidom中标准化XML文本节点

[英]Normalize XML text node in Python minidom

I want to insert this string: 我想插入这个字符串:

No, on the 5<Font Script="super">th</Font>

as a Text Node in XML by xml.dom.minidom createTextNode(), however, after I writexml() to a file, the signs: 通过xml.dom.minidom createTextNode()作为XML中的文本节点,但是,当我将xml()写入文件后,标志:

< > "

turns to: 变成:

No, on the 5&lt;Font Script=&quot;super&quot;&gt;th&lt;/Font&gt;

How can I avoid this? 如何避免这种情况? Thanks. 谢谢。

A part of my code: 我的代码的一部分:

impl = minidom.getDOMImplementation()
dom = impl.createDocument(None, None, None)
TextTextNode = dom.createTextNode(text.decode("utf-8"))
Text = dom.createElement("Text")
Text.appendChild(TextTextNode)
fileToWrite =  codecs.open(output, 'w', encoding='utf-8')
dom.writexml(fileToWrite, indent=" ", addindent=" ", newl="\n", encoding='utf-8')
fileToWrite.close() 

There is a sample for this by the cinecanvase specification: cinecanvase规范为此提供了一个示例:

<Text HAlign=”left” HPosition=”10.2” VAlign=”bottom” VPosition=”10.0”> This <Font Script=”super”>word </Font>is superscript </Text > 

I need insert the <Font>..</Font> into another element, the . 我需要将<Font>..</Font>插入到另一个元素中。

I'm not familiar with that format, but that thing looks like an XML node. 我不熟悉这种格式,但是那东西看起来像一个XML节点。 Try this: 尝试这个:

from xml.dom import minidom
import codecs

output = "test.xml"
text="No, on the 5"


impl = minidom.getDOMImplementation()
dom = impl.createDocument(None, None, None)
FontNode = dom.createElement("Font")
FontNode.setAttribute('Script', 'super')
FontNode.appendChild(dom.createTextNode('th'))
Text = dom.createElement("Text")
TextTextNode = dom.createTextNode(text.decode("utf-8"))
Text.appendChild(TextTextNode)
Text.appendChild(FontNode)
fileToWrite =  codecs.open(output, 'w', encoding='utf-8')
Text.writexml(fileToWrite, indent=" ", addindent=" ", newl="\n")
fileToWrite.close() 

That outputs: 输出:

 <Text>
  No, on the 5
  <Font Script="super">th</Font>
 </Text>

Be aware that what you want to write a tree in a file (when you call writexml ) you need to call the writexml method with your XML's tree root (you were calling it with dom , not with your root node) 请注意,要在文件中写入树的内容(当您调用writexml时 )需要使用XML的树根(您使用dom而不是根节点来调用)来调用writexml方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM