Skip to content Skip to sidebar Skip to footer

Scala Html Parser Object Usage

I am using the HTML parser to parse an HTML string: import nu.validator.htmlparser.{sax,common} import sax.HtmlParser import common.XmlViolationPolicy val source = Source.fromStri

Solution 1:

It's unclear where your HTML5Parser class comes from, but I'm going to assume it's the one in this example (or something similar). In that case your htmlObject is just a scala.xml.Node. First for some setup:

val source = Source.fromString(
  "<html><head/><body><divclass='main'><span>test</span></div></body></html>"
)

val htmlObject = html.loadXML(source)

Now you can do the following, for example:

scala> htmlObject.child(1).label
res0:String = body

scala> htmlObject.child(1).child(0).child(0).textres1:String = test

scala> (htmlObject \\ "span").textres2:String = test

scala> (htmlObject \ "body" \ "div" \ "span").textres3:String = test

scala> (htmlObject \\ "div").head.attributes.asAttrMap
res4: Map[String,String] = Map(class -> main)

Etcetera.

Post a Comment for "Scala Html Parser Object Usage"