How to iterate over a list of children found inside a single scala.xml.Node

I pull some markup from the url and return one scala.xml.Node, for example ...

  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 scroll through each child element inside the markup, and what I have so far seems to only print 2x (still this is a huge amount of html returned). 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!

+3
source share
4 answers

Anyway, 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 submit it, for example:

<html>
    <head>
        <title>Welcome</title>
    </head>
    <body>
        <div>
            <p>Foo</p>
        </div>
    </body>
</html>

He will produce:

Welcome
Foo
+5
source

As a simple solution, you could say

markup.child.map { child =>
  // child is a scala.xml.Node
}

and maybe use recursion, depending on what you want to do.

+4

, ,

for(child<-markup.child){
 // child is a scala.xml.Node
}
+3

( ) :

import scala.xml._
import scala.annotation.tailrec

object XmlTagLister extends App {
  require(args.length == 1, "You must provide an XML filename to be analyzed.")

  val data = XML.loadFile(args(0))

  @tailrec
  def collectTags(elems: List[Node], tags: Set[String]): Set[String] =
    elems match {
      case h :: t => collectTags((h \ "_").theSeq.toList ::: t, tags + h.label)
      case Nil => tags
    }

  val allTags = collectTags((data \ "_").toList, Set())

  allTags foreach println
}

:

onetag
AnotherTag
anothertag

( XML )

0

Source: https://habr.com/ru/post/1786683/


All Articles