漂亮的打印-XML Spark / Scala中的单一记录



我在单个记录中获取XML数据。我们具有内置的XML解析器功能,但必须以缩进/美化的方式提供XML文件。

我是Spark和Scala的新手。因此

示例输入:

<?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>

预期输出:

<?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>
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>

上面转换为scala.xml.Elem,我将其留给您。在Scala中有一个PrettyPrinter类,请参见此示例Scala Cook Book

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

最新更新