如何使用 xml.dom 中的 minidom 将内部内容作为字符串获取? [英] How to get inner content as string using minidom from xml.dom?
问题描述
我的 xml 文件中有一些文本标签(使用 popplers-utils 中的 pdftohtml 将 pdf 转换为 xml),如下所示:
<text top="525" left="170" width="603" height="16" font="1">...旧大书的一部分</text><text top="546" left="128" width="645" height="16" font="1">有很多页和一些<i>纯"文本中的斜体文本</i>;和越来越多的文本<text top="566" left="128" width="642" height="16" font="1">等等...</text>
并且我可以使用此示例代码获取包含文本标记的文本:
导入字符串从 xml.dom 导入 minidomxmldoc = minidom.parse('../test/text.xml')itemlist = xmldoc.getElementsByTagName('text')some_tag = itemlist[node_index]output_text = some_tag.firstChild.nodeValue# 如果 <i> 里面有所有的文字我可以得到它output_text = some_tag.firstChild.firstChild.nodeValue# 但没有如果 <i></i>只包装字符串的一个单词
但是如果nodeValue"包含另一个标签 (<i> 或 <b>...)
并且无法获取对象,我将无法获取它
将所有文本作为纯字符串(如 javascript innerHTML 方法)或递归到子标签(即使它们包含一些单词而不是整个 nodeValue)的最佳方法是什么?
谢谢
**问题:如何使用 minidom 获取字符串形式的内部内容
这是一个递归解决方案,例如:
def getText(nodelist):# 迭代所有节点聚合 TEXT_NODErc = []对于节点列表中的节点:如果 node.nodeType == node.TEXT_NODE:rc.append(node.data)别的:# 递归rc.append(getText(node.childNodes))返回 ''.join(rc)xmldoc = minidom.parse('../test/text.xml')nodelist = xmldoc.getElementsByTagName('text')# 迭代 <text ..>...</text>节点列表对于节点列表中的节点:打印(getText(node.childNodes))
<块引用>
输出:
.. 旧大书的一部分在纯"文本和越来越多的文本中包含许多页面和一些斜体文本等等...
使用 Python 测试:3.4.2
I have some text tags in my xml file (pdf converted to xml using pdftohtml from popplers-utils) that looks like this:
<text top="525" left="170" width="603" height="16" font="1">..part of old large book</text>
<text top="546" left="128" width="645" height="16" font="1">with many many pages and some <i>italics text among 'plain' text</i> and more and more text</text>
<text top="566" left="128" width="642" height="16" font="1">etc...</text>
and I can get text envolved with text tag with this sample code:
import string
from xml.dom import minidom
xmldoc = minidom.parse('../test/text.xml')
itemlist = xmldoc.getElementsByTagName('text')
some_tag = itemlist[node_index]
output_text = some_tag.firstChild.nodeValue
# if there is all text inside <i> I can get it by
output_text = some_tag.firstChild.firstChild.nodeValue
# but no if <i></i> wrap only one word of the string
but I can not get "nodeValue" if it contents another tag (<i> or <b>...)
inside and can not get object either
What is the best way to get all text as plain string like javascript innerHTML method or recurse into child tags even if they wraps some words and not entire nodeValue?
thanks
**Question: How to get inner content as string using minidom
This is a Recursive Solution, for instance:
def getText(nodelist):
# Iterate all Nodes aggregate TEXT_NODE
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
else:
# Recursive
rc.append(getText(node.childNodes))
return ''.join(rc)
xmldoc = minidom.parse('../test/text.xml')
nodelist = xmldoc.getElementsByTagName('text')
# Iterate <text ..>...</text> Node List
for node in nodelist:
print(getText(node.childNodes))
Output:
..part of old large book with many many pages and some italics text among 'plain' text and more and more text etc...
Tested with Python: 3.4.2
这篇关于如何使用 xml.dom 中的 minidom 将内部内容作为字符串获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!