我正在单个记录中获取XML数据。我们具有内置的XML解析器功能,但必须以缩进/美化的方式提供XML文件。
我是Spark和Scala的新手。因此,有关如何在spark / scala中美化/缩进/漂亮地打印XML文件的任何输入都会有所帮助
示例输入:
<?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>
答案 0 :(得分:3)
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)