Scala - XML PrettyPrinter.format() 给出 noSuchMethodError

Scala - XML PrettyPrinter.format() giving noSuchMethodError

我有这段代码应该将 XML.Elem object 转换为可读且正确缩进的文本,但是当我 运行 它总是给我一个错误,并且我不明白为什么,因为据我所知和搜索,我正在使用 PrettyPrinter class 因为它应该是。

val xml = createXML(production_list: ProductionList)

// Using the PrettyPrinter class to make sure the XML is “human readable.”  
val prettyPrinter = new scala.xml.PrettyPrinter(80, 2)
val prettyXml = prettyPrinter.format(xml)

XML.save(filename, scala.xml.XML.loadString(prettyXml), "UTF-8", true, null)

这里是创建XML方法的header:

def createXML(production_list: ProductionList): xml.Elem = {(...)}

这里是错误:

java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd()Ljava/lang/Object;
  at scala.xml.NamespaceBinding.fromPrefixList(NamespaceBinding.scala:48)
  at scala.xml.NamespaceBinding.shadowRedefined(NamespaceBinding.scala:53)
  at scala.xml.NamespaceBinding.buildString(NamespaceBinding.scala:71)
  at scala.xml.Utility$.serialize(Utility.scala:218)
  at scala.xml.PrettyPrinter.traverse(PrettyPrinter.scala:151)
  at scala.xml.PrettyPrinter.format(PrettyPrinter.scala:209)
  at scala.xml.PrettyPrinter$$anonfun$format.apply(PrettyPrinter.scala:244)
  at scala.xml.PrettyPrinter$$anonfun$format.apply(PrettyPrinter.scala:244)
  at scala.xml.Utility$.sbToString(Utility.scala:33)
  at scala.xml.PrettyPrinter.format(PrettyPrinter.scala:244)
  ...

我创建的片段 XML:

<?xml version='1.0' encoding='UTF-8'?>
<Schedule xsi:schemaLocation="http://www.dei.isep.ipp.pt/ip_2017 ip_2017_out.xsd " xmlns="http://www.dei.isep.ipp.pt/ip_2017" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <TaskSchedule order="ORD_1" productNumber="1" task="TSK_1" start="0" end="100">
                <PhysicalResources>
              <Physical id="PRS_1"/><Physical id="PRS_3"/><Physical id="PRS_4"/>
                </PhysicalResources>
                <HumanResources>
              <Human name="Antonio"/><Human name="Susana"/><Human name="Maria"/>
                </HumanResources>
            </TaskSchedule><TaskSchedule order="ORD_1" productNumber="1" task="TSK_3" start="100" end="260">
                <PhysicalResources>
              <Physical id="PRS_1"/><Physical id="PRS_4"/><Physical id="PRS_7"/>
                </PhysicalResources>
                <HumanResources>
              <Human name="Antonio"/><Human name="Maria"/><Human name="Manuel"/>
                </HumanResources>
            </TaskSchedule>
    </Schedule>

这可以通过更改 XML.save 的一些参数来解决。拿走:

XML.save(xmlPath, XML.loadString(printer.format(prettyXml, scala.xml.TopScope)) , "UTF-8", true, null)

和平。安息吧。