0
votes

I am facing a problem with castor while trying to parse an xml which contains same name for the parent and child node... Here are the details:

This is the part of the mapping file:

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

And this is the xml which I am trying to parse:

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

And this is the error logs related with the above stuff:


[#|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)
|#]

Is it a bug or some other problem that I need to change something in the mapping? There is no way to change the xml and its schema as I am getting it from a third party. if there is no way to handle this changing the mapping and so on, shall i use another parser?

1

1 Answers

0
votes

I tried to edit the question and I don't have enough rep to comment but this question seems to be the same as https://stackguides.com/questions/16650013/parsing-with-castor-xml-parser.

Over there, a comment was made that castor cannot parse elements where the child and parent have the same name. With that, you'll have to change one or the other.