使用Python xmldoc库将元素附加到表DOM元素

时间:2015-09-15 23:33:59

标签: python xml dom

我正在使用python xmldoc来创建节点并附加它们:

codeblocks = xmldoc.getElementsByTagName("code")
for ii, codeblock in enumerate(codeblocks):
    if codeblock.getAttribute('class') == 'language-citb':
        codeTable = xmldoc.createElement('table')
        codeTextArea = xmldoc.createElement('textarea')
        codeTable.appendChild(codeTextArea)
        codeblock.parentNode.parentNode.replaceChild(codeTable,
                                                     codeblock.parentNode)
        #The location of appendChild does not change anything
        #codeTable.appendChild(codeTextArea)

orignal dom结构:

<pre>
    <code></code>
</pre>

预期的dom结构:

<table>
    <textarea></textarea>
</table>

产生dom结构:

<textarea></textarea>
<table></table>

为什么我会获得最后的DOM结构?我希望在那之前得到那个。

1 个答案:

答案 0 :(得分:0)

我必须通过

设置textarea的值
{{1}}

防止文本值出现一些溢出 并在表元素中创建'tr'和'td'元素,使其正确显示。