Castor XML与子节点映射相同的名称

时间:2013-11-20 06:08:37

标签: java xml-parsing castor

我在尝试解析包含父节点和子节点相同名称的xml时遇到了castor的问题......以下是详细信息:

这是映射文件的一部分:

<class name="com.somepackage.Item">
    <field name="id" type="java.lang.long">
      <bind-xml name="itemiD" node="element"/>
    </field>
    <field name="amount" type="com.somepackage.Amount">
      <bind-xml name="amount" />
    </field>
</class>
<class name="com.somepackage.Amount" >
  <field name="amount" type="java.lang.String">
    <bind-xml name="amount" />
  </field>
  <field name="currency" type="java.lang.String">
    <bind-xml name="currency" />
  </field>
</class>

这是我要解析的xml:

<?xml version="1.0" encoding="UTF-8"?>
<list xmlns="http://someurl">
<item>
<itemID>42425</itemID>
<amount>
  <amount>10.00</amount>
  <currency>EUR</currency>
</amount>
<item>
</list>

这是与上述内容相关的错误日志:


[#|2013-11-20T13:48:54.670+0800|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=32;_ThreadName=Thread-2;|13-11-20 13:48:54 ERROR util.XMLConvertUtil http-thread-pool-8181(1) - org.exolab.castor.xml.MarshalException: Illegal Text data found as child of: amount
  value: "10.00"{File: [not available]; line: 14; column: 29}
org.exolab.castor.xml.MarshalException: Illegal Text data found as child of: amount
  value: "10.00"{File: [not available]; line: 14; column: 29}
  at org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(Unmarshaller.java:761)
  at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:727)
  at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:616)
  at some.package.XMLConvertUtil.xmlToObject(XMLConvertUtil.java:58)
  at some.package.ConverUtil.xmlToPojo(ConverUtil.java:31)
  at some.package.XMLParser.process(XMLParser.java:89)
  at some.package.XMLParser.doPost(XMLParser.java:1063)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:662)
Caused by: org.xml.sax.SAXException: Illegal Text data found as child of: amount
  value: "10.00"
  at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:874)
  at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1159)
  at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2938)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
  at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
  at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
  at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
  at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:715)
  ... 31 more
Caused by: org.xml.sax.SAXException: Illegal Text data found as child of: amount
  value: "10.00"
  at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:874)
  at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1159)
  at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2938)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
  at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
  at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
  at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
  at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:715)
  at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:616)
  at some.package.XMLConvertUtil.xmlToObject(XMLConvertUtil.java:58)
  at some.package.ConverUtil.xmlToPojo(ConverUtil.java:31)
  at some.package.XMLParser.process(XMLParser.java:89)
  at some.package.XMLParser.doPost(XMLParser.java:1063)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.gr|#]

[#|2013-11-20T13:48:54.671+0800|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=32;_ThreadName=Thread-2;|izzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:662)
|#]

我需要在映射中更改某些内容的错误或其他问题吗?当我从第三方获取它时,无法更改xml及其架构。如果没有办法处理这个改变映射等等,我应该使用另一个解析器吗?

1 个答案:

答案 0 :(得分:0)

我尝试编辑问题但我没有足够的代表发表评论,但这个问题似乎与https://stackoverflow.com/questions/16650013/parsing-with-castor-xml-parser相同。

在那里,有人评论说,castor无法解析子元素和父元素具有相同名称的元素。有了它,你将不得不改变其中一个。