- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
标题不言自明,在将其标记为重复之前,请考虑我已经检查了 this answer它对我不起作用,因为我什至没有在 sys.stdout
中获得正确的格式,不仅是在写入文件时。所以我有以下 xml (test.xml):
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www...">
<soap:Body>
<SubmitTransaction xmlns="http://www.">
<Authentication>
</Authentication>
<Transaction>
<DataFields>
</DataFields>
</Transaction>
</SubmitTransaction>
</soap:Body>
</soap:Envelope>
以及以下代码:
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse("test.xml", parser)
def get_data_fields():
for node in tree.iter():
if 'DataFields' in node.tag:
return node
a = get_data_fields()
field = etree.Element('Field_1')
child_1 = etree.Element('FieldName')
child_2 = etree.Element('FieldValue')
child_3 = etree.Element('FieldIndex')
child_1.text = 'dateTime'
child_2.text = '2016-07-29T12:00:00'
child_3.text = '1'
for i in [child_1, child_2, child_3]:
field.append(i)
a.append(field)
s = etree.tostring(tree, pretty_print=True)
print(s.decode('utf-8'))
输出
<soap:Envelope xmlns:soap="http://www...">
<soap:Body>
<SubmitTransaction xmlns="http://www.">
<Authentication>
</Authentication>
<Transaction>
<DataFields>
<Field_1><FieldName>dateTime</FieldName><FieldValue>2016-07-29T12:00:00</FieldValue><FieldIndex>1</FieldIndex></Field_1></DataFields>
</Transaction>
</SubmitTransaction>
</soap:Body>
</soap:Envelope>
预期
<soap:Envelope xmlns:soap="http://www...">
<soap:Body>
<SubmitTransaction xmlns="http://www.">
<Authentication>
</Authentication>
<Transaction>
<DataFields>
<Field_1>
<FieldName>dateTime</FieldName>
<FieldValue>2016-07-29T12:00:00</FieldValue>
<FieldIndex>1</FieldIndex>
</Field_1>
</DataFields>
</Transaction>
</SubmitTransaction>
</soap:Body>
</soap:Envelope>
我真的不明白为什么我添加的新字段没有按预期格式化,因为如果我只打印 field
,一切看起来都很好:
s = etree.tostring(root, pretty_print=True)
print(s.decode('utf-8'))
#<Field_1 xmlns="http://www." xmlns:soap="http://www...">
# <FieldName>dateTime</FieldName>
# <FieldValue>2016-07-29T12:00:00</FieldValue>
# <FieldIndex>1</FieldIndex>
#</Field_1>
注意:我使用的是 python 3.4(这就是我必须 .decode('utf-8')
的原因,否则我只会得到字节文字)。
最佳答案
如果您在 a = get_data_fields()
之后添加此行,它会起作用:
a.text = None
lxml 不能总是确定哪些空格是可忽略的,因此在某些情况下需要手动删除空格。
参见 http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output :
If you want to be sure all blank text is removed from an XML document (or just more blank text than the parser does by itself), you have to use either a DTD to tell the parser which whitespace it can safely ignore, or remove the ignorable whitespace manually after parsing, e.g. by setting all tail text to None:
关于python - 添加新元素后,lxml 库不向树添加换行符或缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31659163/
我正在用 Java 编写代码,并且使用 Vaadin 8 扩展。 我有一个 Vaadin 组合盒,效果很好。但我不仅想从组合框中选择项目,还想选择书面输入。这意味着我想使用组合框作为下拉菜单以及文本编
我正在尝试将 AJAX 添加到 JQuery ListView 中并呈黄色闪烁,但我似乎无法使其正常工作。谁能指出我正确的方向? http://jsfiddle.net/zFybm/ 最佳答案 根据
我有这个样式表: .pixel{ position: absolute; height: 10px; width: 10px; background-color: #f
这是我用来将新行推送到容器的一行代码: this.$el.append(new ItemView(item).render().el); 其中item是Backbone.js model,render
我正在尝试在 anguar.js 中制作一些测试应用程序,但遇到了问题。我的 js 文件包含: live = angular.module('live',[]); live.controller('p
如何绑定(bind)页面加载后创建的新元素? 我有这样的东西 system = function() { this.hello = function() { alert
html5 新元素(页眉、导航、页脚等)在 IE 中不工作 最佳答案 您需要包含 HTML5 shiv 脚本以允许在旧版 IE 浏览器中设置 HTML5 元素的样式:http://code.googl
我是一名优秀的程序员,十分优秀!