Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何确定节点是否有子节点_Xml_Scala_Xpath - Fatal编程技术网

Xml 如何确定节点是否有子节点

Xml 如何确定节点是否有子节点,xml,scala,xpath,Xml,Scala,Xpath,我正在做一些HTMLscraping 我使用并从中提取数据,将HTML页面转换为有效的XML 我需要检查给定的是否有子节点,因为在每种情况下程序的流程都会不同。是否有任何hasChildren等效项?我在文档中没有找到类似的内容。如下所示: <td style="width: 1%; padding-right: 5px;"> </td> 找出它是否有任何子节点的最简单方法是什么?我以前尝试过但没有成功的方法是: node isEmpty 在本例中,节点是我的 现

我正在做一些
HTML
scraping

我使用
并从中提取数据,将
HTML
页面转换为有效的
XML

我需要检查给定的
是否有子节点,因为在每种情况下程序的流程都会不同。是否有任何
hasChildren
等效项?我在文档中没有找到类似的内容。
如下所示:

<td style="width: 1%; padding-right: 5px;">
</td>


找出它是否有任何子节点的最简单方法是什么?

我以前尝试过但没有成功的方法是:

node isEmpty
在本例中,
节点
是我的

现在我所做的实际上是尝试获取我期望的第一级数据,并查看它是否为空

node \\ "span" isEmpty

这给了我想要的结果。我不知道这是否是最好的方法,但我把答案留在这里作为一个建议,以防没有人回答。

实际上,
org.htmlcleaner.TagNode
有方法
public boolean haschilds()
。在您的情况下,如果“td”有孩子,它将返回:

import org.htmlcleaner._
val cleaner:HtmlCleaner  = new HtmlCleaner()
val html = """
             |<html>
             |
             |<head />
             |
             |<body>
             |      <table>
             |          <tbody>
             |              <tr>
             |                  <td style="width: 1%; padding-right: 5px;"></td>
             |              </tr>
             |          </tbody>
             |      </table>
             |  </body>
             |</html>
           """.stripMargin

    val td = cleaner.clean(html).findElementByName("td", true)
    td.hasChildren  //returns false
import org.htmlcleaner_
val cleaner:HtmlCleaner=新的HtmlCleaner()
val html=“”
|
|
|
|
|
|      
|          
|              
|                  
|              
|          
|      
|  
|
“.stripMargin”
val td=cleaner.clean(html).findElementByName(“td”,true)
td.haschilds//返回false

类似的方法应该可以:

import scala.xml._
def isEmpty(node: Node) = node.child. // All children
  // Filter out empty text nodes
  filter {childNode => !childNode.isInstanceOf[Text] || !childNode.text.trim.isEmpty}.
  isEmpty
在答复中:

scala> import scala.xml._
import scala.xml._

scala> def isEmpty(node: Node) = node.child. // All children
     |       // Filter out empty text nodes
     |       filter {childNode => !childNode.isInstanceOf[Text] || !childNode.text.trim.isEmpty}.
     |       isEmpty
isEmpty: (node: scala.xml.Node)Boolean

scala> val emptyTd = <td style="width: 1%; padding-right: 5px;">
     | </td>
emptyTd: scala.xml.Elem = 
<td style="width: 1%; padding-right: 5px;">
</td>

scala> val nonEmptyTd1 = <td style="width: 1%; padding-right: 5px;">
     | Lorem ipsum
     | </td>
nonEmptyTd1: scala.xml.Elem = 
<td style="width: 1%; padding-right: 5px;">
Lorem ipsum
</td>

scala> val nonEmptyTd2 = <td style="width: 1%; padding-right: 5px;">
     | <br />
     | </td>
nonEmptyTd2: scala.xml.Elem = 
<td style="width: 1%; padding-right: 5px;">
<br/>
</td>

scala> isEmpty(emptyTd)
res0: Boolean = true

scala> isEmpty(nonEmptyTd1)
res1: Boolean = false

scala> isEmpty(nonEmptyTd2)
res2: Boolean = false
scala>导入scala.xml_
导入scala.xml_
scala>def isEmpty(node:node)=node.child.//所有儿童
|//筛选出空文本节点
|筛选器{childNode=>!childNode.isInstanceOf[Text]| |!childNode.Text.trim.isEmpty}。
|空空如也
isEmpty:(节点:scala.xml.node)布尔值
scala>val emptyytd=
| 
emptyTd:scala.xml.Elem=
scala>val nonEmptyTd1=
|同侧眼睑
| 
NoneEmptyTD1:scala.xml.Elem=
乱数假文
scala>val nonEmptyTd2=
|
| nonEmptyTd2:scala.xml.Elem=
scala>isEmpty(emptyTd) res0:Boolean=true scala>isEmpty(非EmptyTD1) res1:Boolean=false scala>isEmpty(非EmptyTD2) res2:Boolean=false