0

I am using javax xml for parsing XML, what I want is to include parent in all children.

<a>
   <b value1 = "b1", value2 = "b2">
       <c value1 = "c1", value2 = "c2" />
       <c value1 = "c3", value2 = "c4" />
   </b>
   <b value1 = "b3">
       <c value1="c5" />
       <c value1 ="c6" />
   </b>
</a>

I want result to be in form like:

Set((b1,c1,c2), (b1,c3,c4), (b3, c5, ""), (b3, c6, "")) 
ashawley
  • 4,195
  • 1
  • 27
  • 40
arglee
  • 1,374
  • 4
  • 17
  • 30

1 Answers1

2

Here is how I was able to achieve this using XML literals in Scala. It is not the most efficient approach, but it accomplishes the goal you specified:

scala> val xml: scala.xml.Elem = <a>
  <b value1="b1" value2="b2">
    <c value1="c1" value2="c2"/>
    <c value1="c3" value2="c4"/>
  </b>
  <b value1="b3">
    <c value1="c5"/>
    <c value1="c6"/>
  </b>
</a>

scala> val bs = xml\ "b"
bs: scala.xml.NodeSeq = <b value1="b1" value2="b2">
    <c value1="c1" value2="c2"/>
    <c value1="c3" value2="c4"/>
  </b><b value1="b3">
    <c value1="c5"/>
    <c value1="c6"/>
  </b>

scala> val gatheringNodes = bs.map { b =>
  val cs = b\"c"
  b\"@value1" -> cs.map(c => (c\"@value1", c\"@value2"))
}
gatheringNodes: scala.collection.immutable.Seq[(scala.xml.NodeSeq, scala.collection.immutable.Seq[(scala.xml.NodeSeq, scala.xml.NodeSeq)])] = List((b1,List((c1,c2), (c3,c4))), (b3,List((c5,), (c6,))))

scala> val finalOutput = gatheringNodes.flatMap { case(b, cs) =>
    cs.map { case(c1, c2) => (b, c1, c2)}
}.toSet
finalOutput: scala.collection.immutable.Set[(scala.xml.NodeSeq, scala.xml.NodeSeq, scala.xml.NodeSeq)] = Set((b1,c1,c2), (b1,c3,c4), (b3,c5,), (b3,c6,))
Tanjin
  • 2,442
  • 1
  • 13
  • 20
  • Hey, Thank you for response, this is absolutely right but i dont know how many columns are there in xml there may be two "c" tags or more than that also. if these are variable then how can we do that ? – arglee May 09 '17 at 09:26
  • I've adjusted the code (see val finalOutput) to handle the scenario with multiple 'c' tags. – Tanjin May 09 '17 at 09:56