• python lxml库生成xml文件-节点命名空间问题


    lxml库,处理xml很强大,官方文档:https://lxml.de/tutorial.html#namespaces

    例如:

    我们要生成如下格式的报文:

    <ttt:jesson xmlns:ttt="http://www.hellojesson/ttt" guid="33344555677777777777" version="1.0" xsi="http://www.hahaha.com">
      <ttt:order>
        <ttt:orderhead>
          <ttt:guid/>
        </ttt:orderhead>
      </ttt:order>
    </ttt:jesson>

    就可以参考如下的样例代码实现:

    # 导入库
    import lxml.etree as etree
    
    # 注册指定命名空间
    etree.register_namespace("ttt", "http://www.hellojesson/ttt")
    # 生成根节点 root
    = etree.Element("{http://www.hellojesson/ttt}jesson", xsi="http://www.hahaha.com", guid="33344555677777777777", version="1.0") # 生成子节点: order = etree.SubElement(root, "{http://www.hellojesson/ttt}order") orderhead = etree.SubElement(order, "{http://www.hellojesson/ttt}orderhead") guid = etree.SubElement(orderhead, "{http://www.hellojesson/ttt}guid") # 节点赋值 order.text = "text" orderhead.text = "111" guid.text = "hello nihao" # 输出 查看效果 print(etree.tostring(root, pretty_print=True))
  • 相关阅读:
    HDU 1051 Wooden Sticks (贪心)
    PHP中递归函数的一个常见逻辑问题
    【Android界面实现】使用Canvas对象实现“刮刮乐”效果
    vue2.0
    vuex3
    nodejs中require的路径是一个文件夹时发生了什么
    vue2
    vuex
    echarts
    node21---mongoose
  • 原文地址:https://www.cnblogs.com/hellojesson/p/9565349.html
Copyright © 2020-2023  润新知