<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                首先下載lxml,?[http://www.lfd.uci.edu/~gohlke/pythonlibs/](http://www.lfd.uci.edu/~gohlke/pythonlibs/)?,然后添加引用 fromlxmlimport_elementpathasDONTUSE fromlxmlimportetree 具體示例: **1.添加命名空間** #set namespace nsmap = {"xsi":"http://www.w3.org/2001/XMLSchema-instance"} g_statisticsRoot = etree.Element("DcmStatistics", nsmap = nsmap) **2.添加xml schema引用** #add xsd reference g_statisticsRoot.set("{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation","DcmStatistics.xsd") **3.添加注釋** #add comment, 利用addprevious添加到根節點的前面 comment = etree.Comment("create by jiangong.li") g_statisticsRoot.addprevious(comment) **4.嘗試多種編碼來解析xml** defdecodingXml(xmlFile): ?? tree = None ?? encoding ="utf-8" ??while(True): ??????try: ?????????? parser = etree.XMLParser(remove_blank_text=True, encoding=encoding, remove_comments =False) ?????????? tree = etree.parse(xmlFile, parser) ??????exceptExceptionase: ??????????if(encoding !="gb18030"): ?????????????? encoding ="gb18030" ??????????????continue ??????????else: ?????????????? print("\nPAR XML ERROR, decoding error.") ??????????????break ??????break ??returntree **5.遍歷xml下的所有子節點,不止直屬第一級子節點.iter()** **????for**element**in**root.iter(): ?????????element.tail=None **6.遍歷xml下的第一級子節點.iterchildren()** ??foreinsrcParentNode.iterchildren(): ??????ifeissrcParentNode: ??????????continue ?????? name ="" ??????#statistics node ??????if? e.tag =="element": ?????????? name ="Element" ??????elife.tag =="sequence": ?????????? name ="Sequence" ??????elife.tag =="item": ?????????? name ="Item" ??????else: ?????????? print("\nUnsupported element type: %s\n"%(e.tag)) ?????????? name = e.tag ??????????# Only parse element/sequence/item ? ? ? ? ??continue? ? ? **7.添加子節點到尾部. append()** defgetXmlElement(nodeName, parentNode): ??ifparentNode? == None: ??????raiseException("parent node is None") ?? nodes = parentNode.xpath(&apos;./&apos;+nodeName) ??iflen(nodes) == 0: ?????? node = etree.Element(nodeName) ?????? parentNode.append(node) ??????returnnode ??else: ??????returnnodes[0] **8.格式化成str輸出** etree.tostring(g_statisticsRoot, encoding="UTF-8", xml_declaration=True, pretty_print=True, with_comments=True) **9.保存成xml文件** ? ? ? statisticsResult =? open(g_xmlName,"bw+") ?????? statisticsResult.write(etree.tostring(g_statisticsRoot, encoding="UTF-8", xml_declaration=True, pretty_print=True, with_comments=True)) ?????? statisticsResult.flush() ?????? statisticsResult.close()
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看