如何使用 scala.xml.Node 向上遍历单个子节点
扩展我之前提出的关于如何迭代 scala.xml.Node 中的节点集合的问题,发现 这里
我想更进一步,询问如何在递归函数中查找前一个子节点一旦我遇到特定情况,就会得到一个值
例如(标记在这里)
<html>
<head class="foo">
<title>Welcome</title>
</head>
<body>
<div>
<p>Foo</p>
</div>
</body>
</html>
使用我当前的实现(感谢@knut -arne-vedaa)
def processNode(node: Node) {
if (node.isInstanceOf[Text]) {
if (node.text.contains("Welcome"))
{
//then inside here I want to go up to the prev element (head) and pull the class
}
}
node.child foreach processNode
}
我想添加另一个条件来从类部分中获取文本“foo”
知道我可以在这个 if 语句中添加什么来直接提取这个值吗?另外,如何从该 fx 返回字符串值?带有简单返回线的中断或?
Expanding on a question I asked earlier about how to iterate over a collection of nodes in scala.xml.Node found here
I wanted to take this 1 step further and ask how I could look up to a previous child inside a recursive function to get a value once I hit a specific situation
For example (the markup is here)
<html>
<head class="foo">
<title>Welcome</title>
</head>
<body>
<div>
<p>Foo</p>
</div>
</body>
</html>
With my current implementation (thanks to @knut-arne-vedaa)
def processNode(node: Node) {
if (node.isInstanceOf[Text]) {
if (node.text.contains("Welcome"))
{
//then inside here I want to go up to the prev element (head) and pull the class
}
}
node.child foreach processNode
}
I want to add another conditional to get the text "foo" from inside the class section
Any idea what I could add inside this if statement to pull this value directly? Also how can I return the String value from this fx? a break w/ a simple return line or ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,示例中 Text 节点的父节点实际上是
由于节点没有对其父节点的引用,因此访问它们的一种方法就是在沿着树向下走时将它们传递给您,如下所示:
当然,当您想爬回树上任意位置时,这会变得很笨拙等级。一种方法是将节点包装在具有可选父引用的
SuperNode
中。为了方便起见,在默认没有父节点的情况下,创建一个隐式函数将节点转换为超级节点。
现在您可以在树上导航任意次数,如下所示:
Note that the parent of the Text node in your example is actually the <title> node.
Since Nodes don't have references to their parents, one way to access them is just to pass them with you when walking down the tree, like this:
Of course, this becomes unwieldy when you want to climb back up the tree to an arbitrary level. One approach to this is to wrap your nodes in a
SuperNode
that has an optional parent reference.For convenience, make an implicit function to convert nodes to SuperNodes in the default case of no parent.
Now you can navigate up the tree an arbitrary number of times, like this:
如果你继续朝这个方向前进,你应该使用 XML 拉链。我认为 Scalaz 有一个(它有一个,但我不确定您是否可以使用它与 XML)。
If you keep going in this direction, you should use an XML Zipper. I think Scalaz has one (it has one, but I'm not sure if you can use it with XML).
如果您想对值仅执行某些操作(如果该值位于具有特定属性的节点内),则可以反过来执行:首先查找具有给定属性的所有节点,然后执行相关操作对它们进行处理。
您可以找到具有“foo”的“class”属性的所有节点,如下所示:
编辑:您可以这样使用它:
如果您想从处理中返回一些值,您需要以不同的方式进行操作,但你的问题对此并不清楚。
If you want to do something with the value only if it is inside a node with a certain attribute, you can do it the other way around: first find all nodes with the given attribute, then do the relevant processing on them.
You can find all nodes with a "class" attribute of "foo" like this:
EDIT: You use it like this:
If you want to return some value from the processing you need to do it differently, but your question is not clear in regards to that.