简体   繁体   English

使用castor将xml转换为java对象

[英]xml to java object using castor

What can I do to ignore the <envelope> and <body> tags in unmarshall process using Castor? 如何在使用Castor的解组过程中忽略<envelope><body>标签?

Xml examole: 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 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());
 }
}

Instead of using an input stream, you could use an XMLStreamReader (StAX) as your input. 除了使用输入流,还可以使用XMLStreamReader(StAX)作为输入。 Then advance the XMLStreamReader to the start element event for the content you mapped to. 然后,将XMLStreamReader推进到您映射到的内容的start元素事件。 Then have Castor unmarshal from the XMLStreamReader. 然后让Castor从XMLStreamReader解组。

If Castor does not support StAX then I can show you how to do it with JAXB. 如果Castor不支持StAX,那么我可以向您展示如何使用JAXB。 I lead the EclipseLink JAXB implementation (MOXy). 我负责EclipseLink JAXB实现(MOXy)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM