如何遍历在单个 scala.xml.Node 中找到的子项列表 [英] How to loop over a list of children found inside a single scala.xml.Node
本文介绍了如何遍历在单个 scala.xml.Node 中找到的子项列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从一个 url 中提取一些标记并像这样返回一个 scala.xml.Node ...
def doGoogleSearch(query:String) : scala.xml.Node = {val tmpUrl = "http://www.google.com?q="val tmp = tmpUrl.concat(查询)val url = 新 URL(tmp)val conn = url.openConnectionval 源:输入源 = 新输入源val neo = 新 TagSoupFactoryAdapterval 输入 = conn.getInputStream源.setByteStream(输入)val 标记 = neo.loadXML(sorce)输入.关闭返回标记}
接下来我想遍历标记中的每个子元素,到目前为止我所拥有的似乎只打印了 2x(但这是大量的 html 返回).我在这里错过了什么?
def loopThroughChildren(markup:scala.xml.Node) : String = {for (i <- 0 直到 markup.child.length) {//输出(??}返回 ""}
先谢谢你!
解决方案
无论如何,这里有一个递归函数:
def processNode(node: Node) {if (node.isInstanceOf[Text]) println(node.text)node.child foreach processNode}
这将打印文档中所有文本节点的内容.如果你用例如:
<头><title>欢迎</title>头部><身体><div><p>Foo</p>
</html>
它将产生:
欢迎富
I'm pulling down some markup from a url and returning a single scala.xml.Node like so ...
def doGoogleSearch(query:String) : scala.xml.Node = {
val tmpUrl = "http://www.google.com?q="
val tmp = tmpUrl.concat(query)
val url = new URL(tmp)
val conn = url.openConnection
val sorce:InputSource = new InputSource
val neo = new TagSoupFactoryAdapter
val input = conn.getInputStream
sorce.setByteStream(input)
val markup = neo.loadXML(sorce)
input.close
return markup
}
Next I want to loop through each child element inside the markup and what I have so far only seems to print 2x (yet this is a huge amount of html coming back). What am I missing here?
def loopThroughChildren(markup:scala.xml.Node) : String = {
for (i <- 0 until markup.child.length) {
//println(??
}
return ""
}
Thank you in advance!
解决方案
Anyways, here's a recursive function for you:
def processNode(node: Node) {
if (node.isInstanceOf[Text]) println(node.text)
node.child foreach processNode
}
This will print the contents of all text nodes in the document. If you feed it with e.g.:
<html>
<head>
<title>Welcome</title>
</head>
<body>
<div>
<p>Foo</p>
</div>
</body>
</html>
It will produce:
Welcome
Foo
这篇关于如何遍历在单个 scala.xml.Node 中找到的子项列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文