2007年12月11日 星期二 11:52
我使用minidom来创建一个xml文件,代码如下:
def test():
domImpl = getDOMImplementation()
newDoc = domImpl.createDocument()
eRoot = newDoc.documentElement
eCommand = newDoc.createElement("command")
eCommand.appendChild(newDoc.createTextNode("I'm aaaa\"ddd\""))
eRoot.appendChild(eCommand)
file = open("test.xml", 'w')
writer = codecs.lookup("utf-8")[3](file)
newDoc.writexml(writer, indent = '\t', addindent = '\t', newl =
'\n', encoding = "utf-8")
file.close()
if __name__ == '__main__':
test()
生成的xml用IE打开,正常显示为:
-
I'm aaaa"ddd"
但如果用UltraEdit打开,发现内容中所有的"都被自动转换为转义字符quot;了:
I'm aaaa"ddd"
我不想要自动转换成转义字符,请问在生成xml的时候该如何处理呢?谢谢。
2007年12月11日 星期二 12:15
刚刚网上搜了一下minidom.py的代码:
299 def _write_data(writer, data):
300 "Writes datachars to writer."
301 data = data.replace("&", "&").replace("<", "<")
302 data = data.replace("\"", """).replace(">", ">")
303 writer.write(data)
它在write_xml()的时候,自动把"\""转换成"""了。
请问这该如何是好?
在 07-12-11,Deng Patrick<kedeng19801002在gmail.com> 写道:
> 我使用minidom来创建一个xml文件,代码如下:
>
>
> def test():
> domImpl = getDOMImplementation()
> newDoc = domImpl.createDocument()
> eRoot = newDoc.documentElement
> eCommand = newDoc.createElement("command")
> eCommand.appendChild(newDoc.createTextNode("I'm aaaa\"ddd\""))
> eRoot.appendChild(eCommand)
>
> file = open("test.xml", 'w')
> writer = codecs.lookup("utf-8")[3](file)
> newDoc.writexml(writer, indent = '\t', addindent = '\t', newl =
> '\n', encoding = "utf-8")
> file.close()
>
> if __name__ == '__main__':
> test()
>
> 生成的xml用IE打开,正常显示为:
>
> -
> I'm aaaa"ddd"
>
>
> 但如果用UltraEdit打开,发现内容中所有的"都被自动转换为转义字符quot;了:
>
>
>
> I'm aaaa"ddd"
>
>
>
> 我不想要自动转换成转义字符,请问在生成xml的时候该如何处理呢?谢谢。
>
2007年12月11日 星期二 12:57
服了,不转义怎么能正确得读取进来。 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20071211/814995f7/attachment.html
2007年12月11日 星期二 13:14
哥们口气不太好啊,可能是没听明白我的意思吧。下面是两段代码代码生成同样内容xml的例子,一段是Java的(dom4j),一段是python的(minidom)。
#################Java 代码###########################
import java.io.File;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public final class XmlTester
{
public static void main(String[] args)
{
try
{
Document doc = DocumentHelper.createDocument();
Element eDevice = DocumentHelper.createElement("device");
Element eCmd = DocumentHelper.createElement("cmd");
eCmd.setText("I'm aaa \"bbb\"");
eDevice.add(eCmd);
doc.add(eDevice);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF8");
File xml = new File("java_test.xml");
FileOutputStream out = new FileOutputStream(xml);
XMLWriter xmlWriter = new XMLWriter(out, format);
xmlWriter.write(doc);
xmlWriter.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
##########################Java 生成的文件内容#########################
I'm aaa "bbb"
##########################python 代码################################
def test():
domImpl = getDOMImplementation()
newDoc = domImpl.createDocument(None, "device", None)
eRoot = newDoc.documentElement
eCommand = newDoc.createElement("cmd")
eCommand.appendChild(newDoc.createTextNode("I'm aaa\"ddd\""))
eRoot.appendChild(eCommand)
file = open("test.xml", 'w')
writer = codecs.lookup(CODEC_UTF8)[3](file)
newDoc.writexml(writer, indent = '\t', addindent = '\t', newl =
'\n', encoding = CODEC_UTF8)
file.close()
if __name__ == '__main__':
test()
#########################python生成的文件内容####################
I'm aaa"ddd"
################################################################
我疑惑的是为何minidom做不到dom4j那样,为何要在生成的xml文件里转义字符。
不知道这么写你明白么,望不吝赐教。谢谢。
在 07-12-11,3751<lwm3751在gmail.com> 写道:
> 服了,不转义怎么能正确得读取进来。
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
Zeuux © 2025
京ICP备05028076号