- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要解析一些包含内联 元素的 XML。例如,XML 看起来像这样:
<section>
Fubar, I'm so fubar, fubar and even more <fref bar="baz">fubare</fref>. And yet more fubar.
</section>
如果我现在用 for elem in list(parent): ...
遍历这个结构我只能访问 fref。如果我现在处理 fref,周围的文本当然会丢失,因为文本不是真正的元素。
有人知道如何正确解决这个问题吗?
最佳答案
下面显示了如何使用 lxml
实现这一点。
>>> from lxml.etree import fromstring
>>> tree = fromstring('''<section> Fubar, I'm so fubar, fubar and even more <fref bar="baz">fubare</fref>. And yet more fubar. </section>''')
>>> elem = tree.xpath('/section/fref')[0]
>>> elem.text
'fubare'
>>> elem.tail
'. And yet more fubar. '
>>> elem.getparent().text
" Fubar, I'm so fubar, fubar and even more "
来自 lxml.etree
tutorial :
If you want to read only the text, i.e. without any intermediate tags, you have to recursively concatenate all text and tail attributes in the correct order. Again, the tostring() function comes to the rescue, this time using the method keyword:
>>> from lxml.etree import tostring
>>> tostring(html, method="text")
" Fubar, I'm so fubar, fubar and even more fubare. And yet more fubar. "
还有一种 XPath 方法可以做到这一点,链接页面中对此进行了描述。
关于 python /元素树 : Parsing inline elements w/respect to surrounding text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10180781/
当我选择单词并使用带有 S 的环绕插件时: foobar 变成了 ( foobar ) 如何删除多余的空格,使其变成 (foobar) 我应该在设置中放置什么? 最佳答案 如果您输入 S(,它将被空格
我正在将我的应用程序迁移到 delphi 2009。我的应用程序仍然必须使用大量 AnsiString。在迁移过程中,我发现自己总是在转换: abc := def; 进入: abc := string
当我在 WebStorm 9.0.2 中使用“环绕”功能时,它会删除所选代码中的所有换行符。当我输入“}”来关闭现有代码块时,也会发生同样的情况。 如何告诉 WebStorm 仅缩进代码,但保留现有格
我正在尝试编写一个 vs 代码片段,它将获取所选内容并用 if null 检查将其包围,即 accgrp.CREATEDATE = DateTime.Now; 将变成: if (accgrp.CREA
假设我们有一个具有 n 个不同值的排序整数数组 A,并且给定了一些不包含在 A 中的值 M。 例如:A = [2,3,5,8,10],M = 4。该问题要求我们找到与 M 相关的 x“周围值”,其中
我正在使用 tpope 的 surround.vim .示例: 应用 ysip] 到 [1 0] [0 1] 会产生 [ [1 0] [0 1] ] 相反,我希望输出看起来像 [[1 0] [
我正在使用 vim-surround,我正在尝试替换: 'this text needs to be replaced' 和 **this text needs to be replaced** 我已
我使用 surround vim 插件。当我为 \bf 写一个类似于 的单词时 hello w*orld (*为光标位置)我用 ysiw}a\bf 得到 hello {\bf *world} 有没有更
刚刚安装了环绕插件。 新缓冲区,键入 Hello World。说我想用引号包围它。 在正常模式下,V 进入可视模式,s 进入环绕 除了它没有, s 删除整行。 最佳答案 around.vim 的默认视
当我通过 v 键选择一些代码并按 s 将其包围并用括号括起来时,我收到此错误: surround.vim Visual mode s has been remove in favor of S 如何
我是 Eclipse 新手,主要用于 Java。我之前使用过 IntelliJ Idea,其中可以选择一个扩展 Iteratable(集合、列表等)的变量,并让它生成正确的 foreach 循环。 我
这是第一次接触 Android,我的预览出现问题,顶部和底部都被空白包围,我不确定是什么原因造成的。非常感谢任何帮助。 Activity 主线: 预览: import java.io.IOExc
我目前设置了 .vimrc s 保存我的文件 d 删除在可视模式下突出显示的任何文本 c 将注释光标所在的任何行(或任何一组突出显示的行) 很明显,这会阻止我使用 Surround 的默认命令。有没有
我们可以使用 Snippet Designer 等工具向 Visual Studio 添加“Surrounds With”样式代码片段或者只是“手工”编写适当的 .snippet文件并导入到 visu
我正在阅读一本 html 书,上面写着: b 元素非常简单:包含在开始和结束标记之间的内容与周围的内容有偏移。 那么“偏移自”是什么意思?那个 b 元素不就是把包含的文本加粗了吗? 最佳答案 在 w3
我有以下几行: MIAU,GD & LP MIAU,GDMB & LP 我需要找到第一行,匹配规则:仅当“GFD”和“LP”存在时。 目前我有以下正则表达式: new Regex(@"^(?=.*\b
看完Red Blob Games的精彩article on heaxgon tile maps and their coordinates . 我想知道如何编写一个 SQL 查询来返回围绕居中图 bl
我公司在 Surround SCM 中有一个大型代码库,它是几年前从 SourceSafe 迁移过来的。我们希望转向更适合我们需求的东西,但事实证明,将我们的历史从 Surround 中移除是一项挑战
Surround.vim是一个漂亮的 vim 扩展,它允许你用 、方括号、大括号和几乎任何任意“环绕”字符包围文本 block 。它支持段落和文字环绕,但我经常在视觉模式下使用它。我正在玩 Emacs
可以在一行语句中定义 if-else。 if (isFormed) if (i == 1) System.out.println("i is 1");
我是一名优秀的程序员,十分优秀!