我可以使用

时间:2017-12-09 20:53:18

标签: python python-2.7 lxml elementtree

我正在尝试生成此xml文件:

<?xml version='1.0' encoding='UTF-8'?>
<portlet-preferences>
  <portlet-preference definitionLabel="SharedPreferencesPortlet" user="*">
    <preference name="serverinvoketimeout" description="The description" isModifiable="true" isMultivalued="false" value="360"/>
    <preference name="followchanges" description="The description" isModifiable="true" isMultivalued="false" value="false"/>
</portlet-preference>

使用以下脚本:

from lxml import etree
from lxml.builder import ElementMaker

E = ElementMaker()

format = E.portletpreference(
    E.preference(value="360", isMultivalued="false", isModifiable="true", description="The description", name="serverinvoketimeout"),
    E.preference(value="false", isMultivalued="false", isModifiable="true", description="The description", name="followchanges"),

    user="*",

    definitionLabel="SharedPreferencesPortlet"
    )
    root = E.portletpreferences(
             format,

    )

print(etree.tostring(root, xml_declaration=True, encoding='utf-8', pretty_print=True))

结果:

<?xml version='1.0' encoding='utf-8'?>
<portletpreferences>
  <portletpreference definitionLabel="SharedPreferencesPortlet" user="*">
    <preference isModifiable="true" isMultivalued="false"
name="serverinvoketimeout" value="360" description="The description"/>
    <preference isModifiable="true" isMultivalued="false"
name="followchanges" value="false" description="The description"/>
  </portletpreference>
</portletpreferences>

问题:

lxml适合这种情况吗? 如何获得正确的订单? 例如:<preference name="serverinvoketimeout" description="The description" isModifiable="true" isMultivalued="false" value="360"/>
如何在元素名称中获取minussign? 例如:

<portlet-preferences>

1 个答案:

答案 0 :(得分:0)

  

lxml适合这种情况吗?如何获得正确的订单?例如:

<preference name="serverinvoketimeout" description="The description" isModifiable="true" isMultivalued="false" value="360"/>
  

如何在元素名称中获取minussign?例如:

<portlet-preferences>

正如评论者提到的那样,对属性的排序无关紧要。对于第二个问题,ElementMaker实例需要一个字符串来构造,所以:

In [12]: f = E('portlet-preference', E.preference(value="360", isMultivalued="false", isModifiable="true", description="The description", name="serverinvoketimeout"),
    ...:                             E.preference(value="false", isMultivalued="false", isModifiable="true", description="The description", name="followchanges"),
    ...:       user="*",
    ...:       definitionLabel="SharedPreferencesPortlet")

In [22]: print(etree.tostring(f, encoding=str, pretty_print=True))

<portlet-preference user="*" definitionLabel="SharedPreferencesPortlet">
  <preference value="360" isMultivalued="false" isModifiable="true" description="The description" name="serverinvoketimeout"/>
  <preference value="false" isMultivalued="false" isModifiable="true" description="The description" name="followchanges"/>
</portlet-preference>



In [14]: root = E('portlet-preferences', f)

In [15]: print(etree.tostring(root, encoding=str, pretty_print=True))

<portlet-preferences>
  <portlet-preference user="*" definitionLabel="SharedPreferencesPortlet">
    <preference value="360" isMultivalued="false" isModifiable="true" description="The description" name="serverinvoketimeout"/>
    <preference value="false" isMultivalued="false" isModifiable="true" description="The description" name="followchanges"/>
  </portlet-preference>
</portlet-preferences>