JKC
JKC

Reputation: 2618

Pretty print - XML single record in Spark / Scala

I am getting XML data in a single record. We have a built-in XML parser capability but have to supply the XML file in a indented / beautified way.

I am new to Spark and Scala. So any inputs on how to beautify / indent / pretty print an XML file in spark / scala would be helpful

Sample Input :

<?xml version="1.0" encoding="UTF-8"?><con:REQUEST xmlns:con="http://sample.com/"><Student><StudentID>100234</StudentID><Gender>Male</Gender><Surname>Robert</Surname><Firstname>Mathews</Firstname></Student></con:REQUEST></con:REQUEST>

Expected Output :

<?xml version="1.0" encoding="UTF-8"?>
<con:REQUEST xmlns:con="http://sample.com/">
   <Student>
      <StudentID>100234</StudentID>
      <Gender>Male</Gender>
      <Surname>Robert</Surname>
      <Firstname>Mathews</Firstname>
   </Student>
</con:REQUEST>

Upvotes: 2

Views: 318

Answers (1)

Ram Ghadiyaram
Ram Ghadiyaram

Reputation: 29195

val myxml =<?xml version="1.0" encoding="UTF-8"?><con:REQUEST xmlns:con="http://sample.com/"><Student><StudentID>100234</StudentID><Gender>Male</Gender><Surname>Robert</Surname><Firstname>Mathews</Firstname></Student></con:REQUEST></con:REQUEST>

convert above in to scala.xml.Elem I am leaving it to you. There is a PrettyPrinter class in scala see this example scala cook book

val prettyPrinter = new scala.xml.PrettyPrinter(80, 4)
val myxmlprettyprinted = prettyPrinter.format(myxml)
println(myxmlprettyprinted)

Upvotes: 2

Related Questions