<div style="font-family: Helvetica , Tahoma , Arial , STXihei , "华文细黑" , "Microsoft YaHei" , "微软雅黑" , Heiti , "黑体" , SimSun , "宋体" , sans-serif; font-size: 1.15em; font-weight: 100; color: #2f2f2f;"> <div class="preview"><h1 class="story_title" style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.6; color: inherit; text-rendering: optimizelegibility; font-size: 38.5px;">python處理xml</h1><div class="story_tags"><div class="tag blue label" style="display: inline-block; vertical-align: baseline; line-height: 1; margin-top: 0em; margin-right: 0.125em; margin-bottom: 0em; margin-left: 0.125em; background-color: #3b83c0 !important; border-color: #3b83c0 !important; background-image: none; padding-top: 0.6em; padding-right: 0.8em; padding-bottom: 0.6em; padding-left: 0.8em; color: #ffffff !important; text-transform: none; font-weight: bold; border-radius: 0.2857rem; box-sizing: border-box; font-size: 0.7428rem;">python</div><div class="tag blue label" style="display: inline-block; vertical-align: baseline; line-height: 1; margin-top: 0em; margin-right: 0.125em; margin-bottom: 0em; margin-left: 0.125em; background-color: #3b83c0 !important; border-color: #3b83c0 !important; background-image: none; padding-top: 0.6em; padding-right: 0.8em; padding-bottom: 0.6em; padding-left: 0.8em; color: #ffffff !important; text-transform: none; font-weight: bold; border-radius: 0.2857rem; box-sizing: border-box; font-size: 0.7428rem;">xml</div></div><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"></p><div class="toc"> <ul style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 25px;"> <li style="list-style: none;"><ul style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 25px;"> <li style="list-style: none;"><a href="#e8afbbe58f96xml" data-line="-1" style="color: #0088cc; text-decoration: none;">讀取xml</a><ul style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 25px;"> <li style="list-style: none;"><a href="#e88eb7e58f96dome5afb9e8b1a1" data-line="-1" style="color: #0088cc; text-decoration: none;">獲取dom對象</a></li> <li style="list-style: none;"><a href="#e88eb7e58f96e69687e6a1a3e5afb9e8b1a1" data-line="-1" style="color: #0088cc; text-decoration: none;">獲取文檔對象</a></li> <li style="list-style: none;"><a href="#e88a82e782b9e5b19ee680a7" data-line="-1" style="color: #0088cc; text-decoration: none;">節點屬性</a></li> <li style="list-style: none;"><a href="#e5ad90e88a82e782b9e79a84e8aebfe997ae" data-line="-1" style="color: #0088cc; text-decoration: none;">子節點的訪問</a></li> <li style="list-style: none;"><a href="#e69687e69cace88a82e782b9" data-line="-1" style="color: #0088cc; text-decoration: none;">文本節點</a></li> <li style="list-style: none;"><a href="#e88eb7e58f96e88a82e782b9e5b19ee680a7" data-line="-1" style="color: #0088cc; text-decoration: none;">獲取節點屬性</a></li> <li style="list-style: none;"><a href="#e5b08fe7bb93efbc9ae4bdbfe794a8minidome69da5e8afbbe58f96xmle4b8ade79a84e4bfa1e681af" data-line="-1" style="color: #0088cc; text-decoration: none;">小結:使用minidom來讀取XML中的信息</a></li> </ul> </li> <li style="list-style: none;"><a href="#e7bc96e8be91xml" data-line="-1" style="color: #0088cc; text-decoration: none;">編輯xml</a><ul style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 25px;"> <li style="list-style: none;"><a href="#e7949fe68890dome5afb9e8b1a1" data-line="-1" style="color: #0088cc; text-decoration: none;">生成dom對象</a></li> <li style="list-style: none;"><a href="#e698bee7a4bae7949fe68890e79a84xmle58685e5aeb9" data-line="-1" style="color: #0088cc; text-decoration: none;">顯示生成的xml內容</a></li> <li style="list-style: none;"><a href="#e88eb7e58f96e6a0b9e58583e7b4a0" data-line="-1" style="color: #0088cc; text-decoration: none;">獲取根元素</a></li> <li style="list-style: none;"><a href="#e58583e7b4a0e5908ee6b7bbe58aa0e5ad90e58583e7b4a0e5928ce58583e7b4a0e5b19ee680a7" data-line="-1" style="color: #0088cc; text-decoration: none;">元素後添加子元素和元素屬性</a></li> <li style="list-style: none;"><a href="#e7949fe68890cdatae88a82e782b9" data-line="-1" style="color: #0088cc; text-decoration: none;">生成CDATA節點</a></li> <li style="list-style: none;"><a href="#xmle5afbce585a5e588b0e69687e4bbb6" data-line="-1" style="color: #0088cc; text-decoration: none;">xml導入到文件</a></li> </ul> </li> </ul> </li> </ul> </div> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"></p> <h2 id="e8afbbe58f96xml" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 40px; color: inherit; text-rendering: optimizelegibility; font-size: 31.5px;">讀取xml</h2> <h3 id="e88eb7e58f96dome5afb9e8b1a1" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">獲取dom對象</h3> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span class="mark " style="border: 0; background-color: #ddf3e7; border-radius: 4px; color: #29754d; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">DOM</span>是<code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">Document Object Model</code>的簡稱,它是以對象樹來表示一個XML文檔的方法,使用它的好處就是你能夠很是靈活的在對象中進行遍歷。</p> <ol style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 25px;"> <li><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">方法一:</p></li> </ol> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;"><span class="hljs-keyword" style="color: #93c763; font-weight: bold;">from</span> xml.dom <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">import</span> minidom dom = minidom.parse(<span class="hljs-string" style="color: #ec7600;">"e:\test.xml"</span>) </code></pre> <ol start="2" style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 25px;"> <li><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">方法二:</p></li> </ol> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;"><span class="hljs-keyword" style="color: #93c763; font-weight: bold;">from</span> xml.dom <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">import</span> minidom f = open(<span class="hljs-string" style="color: #ec7600;">"e:\test.xml"</span>, <span class="hljs-string" style="color: #ec7600;">'r'</span>) dom = minidom.parse(<span class="hljs-string" style="color: #ec7600;">"e:\test.xml"</span>) </code></pre> <h3 id="e88eb7e58f96e69687e6a1a3e5afb9e8b1a1" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">獲取文檔對象</h3> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">獲取根元素:</p> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;">root = dom.documentElement </code></pre> <h3 id="e88a82e782b9e5b19ee680a7" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">節點屬性</h3> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">每個結點都有它的 <code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">nodeName</code>, <code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">nodeValue</code>, <code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">nodeType</code> 屬性。 <code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">nodeName</code> 爲結點名字。 <code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">nodeValue</code>爲元素的值,只對文本結點有效, <code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">nodeType</code>爲結點的類型(整型常量)。</p> <pre style="word-wrap: break-word;"><code class="hljs javascript" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;"><span class="hljs-string" style="color: #ec7600;">'ATTRIBUTE_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'CDATA_SECTION_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'COMMENT_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'DOCUMENT_FRAGMENT_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'DOCUMENT_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'DOCUMENT_TYPE_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'ELEMENT_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'ENTITY_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'ENTITY_REFERENCE_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'NOTATION_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'PROCESSING_INSTRUCTION_NODE'</span> <span class="hljs-string" style="color: #ec7600;">'TEXT_NODE'</span> </code></pre> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;">root.nodeName <span class="hljs-comment" style="color: #818e96;">#根元素的名字</span> root.nodeType <span class="hljs-comment" style="color: #818e96;">#根元素的節點類型</span> root.nodeValue <span class="hljs-comment" style="color: #818e96;">#根元素的值,只對文本結點有效</span> </code></pre> <h3 id="e5ad90e88a82e782b9e79a84e8aebfe997ae" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">子節點的訪問</h3> <ol style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 25px;"> <li> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">使用<code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">getElementsByTagName</code>方法<br> <code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">getElementsByTagName</code>方法返回一個列表。(經過搜索全部層次的元素,找到tagname爲給定字符串的全部元素的列表)<br> 語句:<code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">root.getElementsByTagName('title')</code><br> 結果:<code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">[<DOM Element: title at 0x377db48>]</code>。</p> </li> <li> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">childNodes</code> 屬性<br> <code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">childNodes</code>屬性返回某節點的全部直接子節點和直接子元素。即 <span class="mark " style="border: 0; background-color: #ddf3e7; border-radius: 4px; color: #29754d; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;"><code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">childNodes</code>只保存了當前元素的第一層子結點</span>。<br> 語句:<code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">root.childNodes</code><br> 結果:<code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">[<DOM Element: book at 0x349da48>, <DOM Element: book at 0x349ddc8>]</code>。</p> </li> </ol> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">例子:打印根元素的全部直接子元素名字</p> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;"><span class="hljs-keyword" style="color: #93c763; font-weight: bold;">for</span> node <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">in</span> root.childNodes: <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">if</span> node.nodeType == node.ELEMENT_NODE: <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">print</span> node.nodeName </code></pre> <h3 id="e69687e69cace88a82e782b9" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">文本節點</h3> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">獲取文本結點的文本內容可使用: <code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">data</code>屬性<br> 例子:</p> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;">elements = root.getElementsByTagName(<span class="hljs-string" style="color: #ec7600;">"title"</span>) <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">for</span> x <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">in</span> elements: <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">for</span> y <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">in</span> x.childNodes: <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">if</span> y.nodeType == y.TEXT_NODE: <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">print</span> y.data </code></pre> <h3 id="e88eb7e58f96e88a82e782b9e5b19ee680a7" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">獲取節點屬性</h3> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><code style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #2C3E50; font-size: 90%; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">getAttribute</code> 方法獲取節點屬性。</p> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;">nodes = root.getElementsByTagName(<span class="hljs-string" style="color: #ec7600;">"book"</span>) <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">for</span> node <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">in</span> nodes: <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">print</span> x.getAttribute(<span class="hljs-string" style="color: #ec7600;">"id"</span>) </code></pre> <h3 id="e5b08fe7bb93efbc9ae4bdbfe794a8minidome69da5e8afbbe58f96xmle4b8ade79a84e4bfa1e681af" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">小結:使用minidom來讀取XML中的信息</h3> <ol style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 25px;"> <li><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">導入xml.dom.minidom模塊,生成dom對象</p></li> <li><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">獲得文檔對象(根對象)</p></li> <li><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">經過getElementsByTagName()方法和childNodes屬性(還有其它一些方法和屬性)找到要處理的元素</p></li> <li><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">取得元素下文本結點的內容</p></li> </ol> <h2 id="e7bc96e8be91xml" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 40px; color: inherit; text-rendering: optimizelegibility; font-size: 31.5px;">編輯xml</h2> <h3 id="e7949fe68890dome5afb9e8b1a1" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">生成dom對象</h3> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;"><span class="hljs-keyword" style="color: #93c763; font-weight: bold;">from</span> xml.dom <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">import</span> minidom imp = minidom.getDOMImplementation() dom = imp.createDocument(<span class="hljs-keyword" style="color: #93c763; font-weight: bold;">None</span>, <span class="hljs-string" style="color: #ec7600;">'booklist'</span>, <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">None</span>) </code></pre> <h3 id="e698bee7a4bae7949fe68890e79a84xmle58685e5aeb9" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">顯示生成的xml內容</h3> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;">dom.toxml() </code></pre> <h3 id="e88eb7e58f96e6a0b9e58583e7b4a0" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">獲取根元素</h3> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;">root = dom.documentElement </code></pre> <h3 id="e58583e7b4a0e5908ee6b7bbe58aa0e5ad90e58583e7b4a0e5928ce58583e7b4a0e5b19ee680a7" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">元素後添加子元素和元素屬性</h3> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;">root = dom.documentElement book = dom.createElement(<span class="hljs-string" style="color: #ec7600;">"book"</span>) book.setAttribute(<span class="hljs-string" style="color: #ec7600;">"id"</span>, <span class="hljs-string" style="color: #ec7600;">"011"</span>) text = dom.createTextNode(<span class="hljs-string" style="color: #ec7600;">"my book is from England!"</span>) book.appendChild(text) root.appendChild(book) dom.toxml() dom.toprettyxml() </code></pre> <h3 id="e7949fe68890cdatae88a82e782b9" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">生成CDATA節點</h3> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;">data = dom.createCDATASection(<span class="hljs-string" style="color: #ec7600;">"content\nmyContent"</span>) root.appendChild(data) root.toxml() </code></pre> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;">結果:</p> <pre style="word-wrap: break-word;"><code class="language-xml hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;"><span class="hljs-pi" style="color: #818e96;"><?xml version="1.0" ?></span>\n<span class="hljs-tag" style="color: #8cbbad;"><<span class="hljs-title" style="color: #8cbbad; font-weight: bold;">booklist</span>></span>\n\t<span class="hljs-tag" style="color: #8cbbad;"><<span class="hljs-title" style="color: #8cbbad; font-weight: bold;">book</span> <span class="hljs-attribute" style="color: #668bb0;">id</span>=<span class="hljs-value">"011"</span>></span>my book is from England!<span class="hljs-tag" style="color: #8cbbad;"></<span class="hljs-title" style="color: #8cbbad; font-weight: bold;">book</span>></span>\n<span class="hljs-cdata"><![CDATA[content\nmyContent]]></span><span class="hljs-tag" style="color: #8cbbad;"></<span class="hljs-title" style="color: #8cbbad; font-weight: bold;">booklist</span>></span>\n </code></pre> <h3 id="xmle5afbce585a5e588b0e69687e4bbb6" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">xml導入到文件</h3> <pre style="word-wrap: break-word;"><code class="language-python hljs" style="background-color: #D6DBDF; border: 0; border-radius: 4px; color: #e0e2e4; font-size: 90%; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; display: block; overflow-x: auto; background: #282b2e; -webkit-text-size-adjust: none;">f = open(<span class="hljs-string" style="color: #ec7600;">r'e:\text.xml'</span>,<span class="hljs-string" style="color: #ec7600;">'w'</span>) dom.writexml(f) </code></pre> <hr style="margin-top: 20px; margin-right: 0; margin-bottom: 20px; margin-left: 0; border: 0; border-top: 1px solid #eeeeee; border-bottom: 1px solid #ffffff;"> <p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><strong style="font-weight: bold;"><ruby>賀廣福<rp>(</rp><rt>heguangfu</rt><rp>)</rp></ruby></strong>@2015-9-17</p> </div> </div>javascript