[英]printing contents in xml using python
I am trying to convert xml to xslt using the script below. 我正在尝试使用以下脚本将xml转换为xslt。 The conversion is done flawlessly.
转换完美无缺。 Now, i want to write the contents into a new xml file.
现在,我想将内容写入新的xml文件中。 When i try to do so, I get just one line in my xml file.
当我尝试这样做时,我的xml文件中只有一行。 However when I print it in console i get multiline outputs.
但是,当我在控制台中打印它时,会得到多行输出。 Can any one spot what I am doing wrong?
谁能发现我做错了什么?
import lxml.etree as ET
dom = ET.parse("1.xml")
xslt = ET.parse("1.xsl")
transform = ET.XSLT(xslt)
newdom = transform(dom)
print(newdom)
newdom.write("output.xml")
this solved my problem 这解决了我的问题
with open('output.xml', 'wb') as f:
f.write(newdom)
It seems that you're already writing everything to the "output.xml" file. 看来您已经将所有内容写入“ output.xml”文件。 Feel free to post a snippet of your output file, as well as for the input ones.
随时发布输出文件以及输入文件的摘要。
If the only problem is that you want it be structured across multiple lines, compared to be a one-liner, you could just set the pretty print flag to true. 如果唯一的问题是您希望跨多行进行结构化(而不是单行),则只需将漂亮的打印标志设置为true。
newdom.write("output.xml", pretty_print=True)
Output example: 输出示例:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.