lxml etree.fromstring没有创建正确的XML文档

时间:2013-12-05 02:43:54

标签: python xml lxml

大家好,我对lxml有一个小问题 以下是我想要正确格式化为常规xml对象的XML,最终将存储在文件中。 'ToXMLData.xml'的内容

<?xml version="1.0"?><imdata><fvTenant name='t2' status='created,modified'><dbgacEpgToEpg name='dbgacepg2epg2' status='created,modified'></dbgacEpgToEpg><fvBD name='b6' status='created,modified'><fvSubnet ip='56.0.5.1/24' status='created,modified'></fvSubnet><fvRsCtx status='created,modified' tnFvCtxName='ctx2'></fvRsCtx></fvBD><dbgacAnyToEp name='dbgacany2ep2' status='created,modified'></dbgacAnyToEp><dbgacEpgToIp name='dbgacepg2ip2' status='created,modified'></dbgacEpgToIp></fvTenant><fvTenant name='t1' status='created,modified'><dbgacEpgToEpg name='dbgacepg2epg2' status='created,modified'></dbgacEpgToEpg><fvBD name='b6' status='created,modified'><fvSubnet ip='56.0.5.1/24' status='created,modified'></fvSubnet><fvRsCtx status='created,modified' tnFvCtxName='ctx2'></fvRsCtx></fvBD><dbgacAnyToEp name='dbgacany2ep2' status='created,modified'></dbgacAnyToEp><dbgacEpgToIp name='dbgacepg2ip2' status='created,modified'></dbgacEpgToIp></fvTenant>`</imdata>


xmlDataFileNameNew = 'ToXMLData.xml'
xmlDataFileHandler = open(xmlDataFileNameNew,'r+')
xmlstring = xmlDataFileHandler.read()
print xmlstring
root = etree.fromstring(xmlstring)
print root

并且上面代码的输出是

xmlstring >> <?xml version="1.0"?><imdata><fvTenant name='t2' status='created,modified'><dbgacEpgToEpg name='dbgacepg2epg2' status='created,modified'></dbgacEpgToEpg><fvBD name='b6' status='created,modified'><fvSubnet ip='56.0.5.1/24' status='created,modified'></fvSubnet><fvRsCtx status='created,modified' tnFvCtxName='ctx2'></fvRsCtx></fvBD><dbgacAnyToEp name='dbgacany2ep2' status='created,modified'></dbgacAnyToEp><dbgacEpgToIp name='dbgacepg2ip2' status='created,modified'></dbgacEpgToIp></fvTenant><fvTenant name='t1' status='created,modified'><dbgacEpgToEpg name='dbgacepg2epg2' status='created,modified'></dbgacEpgToEpg><fvBD name='b6' status='created,modified'><fvSubnet ip='56.0.5.1/24' status='created,modified'></fvSubnet><fvRsCtx status='created,modified' tnFvCtxName='ctx2'></fvRsCtx></fvBD><dbgacAnyToEp name='dbgacany2ep2' status='created,modified'></dbgacAnyToEp><dbgacEpgToIp name='dbgacepg2ip2' status='created,modified'></dbgacEpgToIp></fvTenant>`</imdata>

root>> <Element imdata at 292aaa0>

对不起,我应该第一次明白。我想要做的是将上面给出的丑陋的xml转换为标准的机器可读的xml。一个例子-----&gt;

<?xml version="1.0" encoding="UTF-8"?>
<imdata>
   <fvTenant name="t2" status="created,modified">
      <dbgacEpgToEpg name="dbgacepg2epg2" status="created,modified" />
      <fvBD name="b6" status="created,modified">
         <fvSubnet ip="56.0.5.1/24" status="created,modified" />
         <fvRsCtx status="created,modified" tnFvCtxName="ctx2" />
      </fvBD>
      <dbgacAnyToEp name="dbgacany2ep2" status="created,modified" />
      <dbgacEpgToIp name="dbgacepg2ip2" status="created,modified" />
   </fvTenant>
   <fvTenant name="t1" status="created,modified">
      <dbgacEpgToEpg name="dbgacepg2epg2" status="created,modified" />
      <fvBD name="b6" status="created,modified">
         <fvSubnet ip="56.0.5.1/24" status="created,modified" />
         <fvRsCtx status="created,modified" tnFvCtxName="ctx2" />
      </fvBD>
      <dbgacAnyToEp name="dbgacany2ep2" status="created,modified" />
      <dbgacEpgToIp name="dbgacepg2ip2" status="created,modified" />
   </fvTenant>
</imdata>

2 个答案:

答案 0 :(得分:1)

打印root为您提供对象的python __str__(在这种情况下,对象是Element实例)。如果你真的想把你的xml树表示为一个字符串(即基本上返回你输入的xml字符串以及一些格式差异),你可以使用etree.tostring函数。

即:

print etree.tostring(root)

答案 1 :(得分:1)

在XML的末尾附近似乎有一个迷失的反引号:

</dbgacEpgToIp></fvTenant>`</imdata>
                          ^

它没有出现在您想要的结果中:

      <dbgacEpgToIp name="dbgacepg2ip2" status="created,modified" />
   </fvTenant>
</imdata>

如果删除了反引号,那么

import lxml.etree as ET
xmlDataFileNameNew = 'ToXMLData.xml'
xmlDataFileHandler = open(xmlDataFileNameNew,'r+')
xmlstring = xmlDataFileHandler.read()
root = ET.fromstring(xmlstring)
print(ET.tostring(root, pretty_print=True))

产量

<imdata>
  <fvTenant name="t2" status="created,modified">
    <dbgacEpgToEpg name="dbgacepg2epg2" status="created,modified"/>
    <fvBD name="b6" status="created,modified">
      <fvSubnet ip="56.0.5.1/24" status="created,modified"/>
      <fvRsCtx status="created,modified" tnFvCtxName="ctx2"/>
    </fvBD>
    <dbgacAnyToEp name="dbgacany2ep2" status="created,modified"/>
    <dbgacEpgToIp name="dbgacepg2ip2" status="created,modified"/>
  </fvTenant>
  <fvTenant name="t1" status="created,modified">
    <dbgacEpgToEpg name="dbgacepg2epg2" status="created,modified"/>
    <fvBD name="b6" status="created,modified">
      <fvSubnet ip="56.0.5.1/24" status="created,modified"/>
      <fvRsCtx status="created,modified" tnFvCtxName="ctx2"/>
    </fvBD>
    <dbgacAnyToEp name="dbgacany2ep2" status="created,modified"/>
    <dbgacEpgToIp name="dbgacepg2ip2" status="created,modified"/>
  </fvTenant>
</imdata>