Scala - XML PrettyPrinter.format()给出noSuchMethodError

时间:2017-06-07 16:45:50

标签: xml scala nosuchmethoderror

我有这段代码应该将XML.Elem对象转换为可读且正确缩进的文本,但是当我运行它时它一直给我一个错误,我无法理解为什么因为据我所知和搜索我正在使用PrettyPrinter类。

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)

这是createXML方法的标题:

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

这是错误:

java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object;
  at scala.xml.NamespaceBinding.fromPrefixList$1(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$2.apply(PrettyPrinter.scala:244)
  at scala.xml.PrettyPrinter$$anonfun$format$2.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>

1 个答案:

答案 0 :(得分:2)

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

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

和平。 RIP ISEP。