使用castor将xml转换为java对象

发布于 2024-10-07 09:36:17 字数 4623 浏览 0 评论 0原文

如何在使用 Castor 的解组过程中忽略 标记?

Xml 示例:

<?xml version="1.0" encoding="UTF-8"?>
<envelope>
  <header>
    <message>consultaTelefonosVigentesSocios</message>
  </header>
  <body>
    <datosTelefonosVigentesSocios>
      <listaTelefonosVigentesSocios>
        <nroInterlocutor>2000393451672</nroInterlocutor>
        <nroContrato>S6125345450573001</nroContrato>
        <nroTelefono>011-4454451-8293</nroTelefono>
        <tipoTelefono>T</tipoTelefono>
        <claseDireccion>Z001</claseDireccion>
        <descClaseDireccion>Correspondencia</descClaseDireccion>
        <marcaEstandar>X</marcaEstandar>
        <nroInterlocutorAsociadoDomicilio>200053945351672</nroInterlocutorAsociadoDomicilio>
      </listaTelefonosVigentesSocios>
      <listaTelefonosVigentesSocios>
        <nroInterlocutor>200053435391672</nroInterlocutor>
        <nroContrato>S612535430573001</nroContrato>
        <nroTelefono>011-44453551-8299</nroTelefono>
        <tipoTelefono>T</tipoTelefono>
        <claseDireccion>Z001</claseDireccion>
        <descClaseDireccion>Correspondencia</descClaseDireccion>
        <marcaEstandar/>
        <nroInterlocutorAsociadoDomicilio>20005543391672</nroInterlocutorAsociadoDomicilio>
      </listaTelefonosVigentesSocios>
      </datosTelefonosVigentesSocios>
  </body>
  <fault>
    <faultactor>servicios.page:consultaTelefonosVigentesSocios</faultactor>
  </fault>
</envelope>

castor 映射文件:

<?xml version="1.0"?>
<mapping>

 <class
  name="ar.com.telefonosSocioByNroContratoService.backend.service.TelefonosVigentesSocios">
  <map-to xml="datosTelefonosVigentesSocios" />
  <field name="listaTelefonosVigentesSocios"
   type="ar.com.telefonosSocioByNroContratoService.backend.service.TelefonoVigenteSocio"
   collection="arraylist">
   <bind-xml name="listaTelefonosVigentesSocios" />
  </field>
 </class>

 <class
  name="ar.com.telefonosSocioByNroContratoService.backend.service.TelefonoVigenteSocio">
  <map-to xml="listaTelefonosVigentesSocios" />
  <field name="nroInterlocutor" type="java.lang.String">
   <bind-xml name="nroInterlocutor" node="element" />
  </field>
  <field name="nroContrato" type="java.lang.String">
   <bind-xml name="nroContrato" node="element" />
  </field>
  <field name="nroTelefono" type="java.lang.String">
   <bind-xml name="nroTelefono" node="element" />
  </field>
  <field name="tipoTelefono" type="java.lang.String">
   <bind-xml name="tipoTelefono" node="element" />
  </field>
  <field name="marcaEstandar" type="java.lang.String">
   <bind-xml name="marcaEstandar" node="element" />
  </field>
  <field name="descClaseDireccion" type="java.lang.String">
   <bind-xml name="descClaseDireccion" node="element" />
  </field>
  <field name="nroInterlocutorAsociadoDomicilio" type="java.lang.String">
   <bind-xml name="nroInterlocutorAsociadoDomicilio" node="element" />
  </field>
 </class>
</mapping>

测试类:

public class TelefonosSocioByNroContratoServiceTest {

 @Test
 public void testUsuarioIntranetListfromXML() throws Exception{
   Mapping mapping= new Mapping();
   ClassPathResource mappingResource = 
    new ClassPathResource("/ar/com/telefonosSocioByNroContratoService/backend/service/telefonosVigenteSocios.map.xml");

      mapping.loadMapping(mappingResource.getURL());
      ClassPathResource inputExample= new ClassPathResource("ar/com/test/castor/consultaTelefonosVigentesSocios.xml");
      Reader reader = new FileReader(inputExample.getFile());

      Unmarshaller unmarshaller = new Unmarshaller(TelefonosVigentesSocios.class);
      unmarshaller.setMapping(mapping);
         TelefonosVigentesSocios telefonosVigentesSocios = (TelefonosVigentesSocios) unmarshaller.unmarshal(reader);
         reader.close();
         Assert.assertNotNull(telefonosVigentesSocios);
         Assert.assertNotNull(telefonosVigentesSocios.getListaTelefonosVigentesSocios());
         Assert.assertTrue("se esperaba  not empty telefonos",!telefonosVigentesSocios.getListaTelefonosVigentesSocios().isEmpty());
 }
}

What can I do to ignore the <envelope> and <body> tags in unmarshall process using Castor?

Xml examole:

<?xml version="1.0" encoding="UTF-8"?>
<envelope>
  <header>
    <message>consultaTelefonosVigentesSocios</message>
  </header>
  <body>
    <datosTelefonosVigentesSocios>
      <listaTelefonosVigentesSocios>
        <nroInterlocutor>2000393451672</nroInterlocutor>
        <nroContrato>S6125345450573001</nroContrato>
        <nroTelefono>011-4454451-8293</nroTelefono>
        <tipoTelefono>T</tipoTelefono>
        <claseDireccion>Z001</claseDireccion>
        <descClaseDireccion>Correspondencia</descClaseDireccion>
        <marcaEstandar>X</marcaEstandar>
        <nroInterlocutorAsociadoDomicilio>200053945351672</nroInterlocutorAsociadoDomicilio>
      </listaTelefonosVigentesSocios>
      <listaTelefonosVigentesSocios>
        <nroInterlocutor>200053435391672</nroInterlocutor>
        <nroContrato>S612535430573001</nroContrato>
        <nroTelefono>011-44453551-8299</nroTelefono>
        <tipoTelefono>T</tipoTelefono>
        <claseDireccion>Z001</claseDireccion>
        <descClaseDireccion>Correspondencia</descClaseDireccion>
        <marcaEstandar/>
        <nroInterlocutorAsociadoDomicilio>20005543391672</nroInterlocutorAsociadoDomicilio>
      </listaTelefonosVigentesSocios>
      </datosTelefonosVigentesSocios>
  </body>
  <fault>
    <faultactor>servicios.page:consultaTelefonosVigentesSocios</faultactor>
  </fault>
</envelope>

castor mapping file:

<?xml version="1.0"?>
<mapping>

 <class
  name="ar.com.telefonosSocioByNroContratoService.backend.service.TelefonosVigentesSocios">
  <map-to xml="datosTelefonosVigentesSocios" />
  <field name="listaTelefonosVigentesSocios"
   type="ar.com.telefonosSocioByNroContratoService.backend.service.TelefonoVigenteSocio"
   collection="arraylist">
   <bind-xml name="listaTelefonosVigentesSocios" />
  </field>
 </class>

 <class
  name="ar.com.telefonosSocioByNroContratoService.backend.service.TelefonoVigenteSocio">
  <map-to xml="listaTelefonosVigentesSocios" />
  <field name="nroInterlocutor" type="java.lang.String">
   <bind-xml name="nroInterlocutor" node="element" />
  </field>
  <field name="nroContrato" type="java.lang.String">
   <bind-xml name="nroContrato" node="element" />
  </field>
  <field name="nroTelefono" type="java.lang.String">
   <bind-xml name="nroTelefono" node="element" />
  </field>
  <field name="tipoTelefono" type="java.lang.String">
   <bind-xml name="tipoTelefono" node="element" />
  </field>
  <field name="marcaEstandar" type="java.lang.String">
   <bind-xml name="marcaEstandar" node="element" />
  </field>
  <field name="descClaseDireccion" type="java.lang.String">
   <bind-xml name="descClaseDireccion" node="element" />
  </field>
  <field name="nroInterlocutorAsociadoDomicilio" type="java.lang.String">
   <bind-xml name="nroInterlocutorAsociadoDomicilio" node="element" />
  </field>
 </class>
</mapping>

Test Class:

public class TelefonosSocioByNroContratoServiceTest {

 @Test
 public void testUsuarioIntranetListfromXML() throws Exception{
   Mapping mapping= new Mapping();
   ClassPathResource mappingResource = 
    new ClassPathResource("/ar/com/telefonosSocioByNroContratoService/backend/service/telefonosVigenteSocios.map.xml");

      mapping.loadMapping(mappingResource.getURL());
      ClassPathResource inputExample= new ClassPathResource("ar/com/test/castor/consultaTelefonosVigentesSocios.xml");
      Reader reader = new FileReader(inputExample.getFile());

      Unmarshaller unmarshaller = new Unmarshaller(TelefonosVigentesSocios.class);
      unmarshaller.setMapping(mapping);
         TelefonosVigentesSocios telefonosVigentesSocios = (TelefonosVigentesSocios) unmarshaller.unmarshal(reader);
         reader.close();
         Assert.assertNotNull(telefonosVigentesSocios);
         Assert.assertNotNull(telefonosVigentesSocios.getListaTelefonosVigentesSocios());
         Assert.assertTrue("se esperaba  not empty telefonos",!telefonosVigentesSocios.getListaTelefonosVigentesSocios().isEmpty());
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还给你自由 2024-10-14 09:36:17

您可以使用 XMLStreamReader (StAX) 作为输入,而不是使用输入流。然后将 XMLStreamReader 前进到您映射到的内容的开始元素事件。然后让 Castor 从 XMLStreamReader 中解组。

如果 Castor 不支持 StAX,那么我可以向您展示如何使用 JAXB 来实现。我领导 EclipseLink JAXB 实现 (MOXy)。

Instead of using an input stream, you could use an XMLStreamReader (StAX) as your input. Then advance the XMLStreamReader to the start element event for the content you mapped to. Then have Castor unmarshal from the XMLStreamReader.

If Castor does not support StAX then I can show you how to do it with JAXB. I lead the EclipseLink JAXB implementation (MOXy).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文