zoukankan      html  css  js  c++  java
  • 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))
  • 相关阅读:
    c++运算符优先级
    C++中宽字符类型(wchar_t)的编码
    标志寄存器综述
    ubuntu 更新源
    windows shell命令相关
    汇编语言-环境搭建(16位)
    linux配置ftp
    ssl协议相关
    boost相关
    ubuntu下编译protobuf
  • 原文地址:https://www.cnblogs.com/hellojesson/p/9565349.html
Copyright © 2011-2022 走看看