XStream自定义转换器

时间:2018-07-16 13:35:41

标签: java xml xstream

我有这个Xml:

<INDIVIDUO>
    <NOME>Some Name</NOME>
    <CPFCNPJ>xxx.xxx.xxx-xx</CPFCNPJ>
    <IMOVEIS>
        <MATRICULA>1511</MATRICULA>
        <MATRICULA>1512</MATRICULA>
        <MATRICULA>1680</MATRICULA>
        <MATRICULA>4698</MATRICULA>
    </IMOVEIS>
</INDIVIDUO>

我有这个班级

publc Class Imoveis {
   String matricula;
}

所以我想将其转换为我的班级Imoveis的列表。

******更新*****

这是我的孔xml:

    <REGISTRO>
        <CANCELAMENTODEINDISPONIBILIDADE>3123123</CANCELAMENTODEINDISPONIBILIDADE>
        <CANCELAMENTO_TIPO>2</CANCELAMENTO_TIPO>
        <CANCELAMENTO_DATA>20/10/2016 18:44:58</CANCELAMENTO_DATA>
        <PROTOCOLOINDISPONIBILIDADE>31312313</PROTOCOLOINDISPONIBILIDADE>
        <DATAPEDIDO>02/07/2015 10:42:41</DATAPEDIDO>
        <NUMERODOPROCESSO>03720158260369</NUMERODOPROCESSO>
        <TELEFONE>11-11111111</TELEFONE>
        <NOMEINSTITUICAO>Nome da Instituicao</NOMEINSTITUICAO>
        <FORUMVARA>MONTE APRAZIVEL - CENTRAL -> 2 Oficio</FORUMVARA>
        <USUARIO>Usuario</USUARIO>
        <EMAIL>emaila@email.br</EMAIL>
        <CANCELAMENTO>
            <INDIVIDUO>
                <NOME>Nome de alguem</NOME>
                <CPFCNPJ>022.266.234-33</CPFCNPJ>
                <IMOVEIS>
                    <MATRICULA>1512</MATRICULA>
                    <MATRICULA>1680</MATRICULA>
                    <MATRICULA>1511</MATRICULA>
                    <MATRICULA>4698</MATRICULA>
                </IMOVEIS>
            </INDIVIDUO>
        </CANCELAMENTO>
    </REGISTRO>

所以我想将该结构保存在Postgresql内部的表中,并且在转换类列表中的标签时遇到问题(我的数据库中有一个表要保存(但每个表都应该是数据库中的一个寄存器) ) Registro,cancelamento和Individuo表可以正常工作。如果XML是这样的:

<IMOVEIS>
  <MATRICULA>1511</MATRICULA>
</IMOVEIS>
<IMOVEIS>
  <MATRICULA>1512</MATRICULA>
</IMOVEIS>
<IMOVEIS>
  <MATRICULA>1680</MATRICULA>
</IMOVEIS>
<IMOVEIS>
  <MATRICULA>4698</MATRICULA>
</IMOVEIS>

但是我不能更改xml:/

1 个答案:

答案 0 :(得分:0)

我希望这个JAXB示例对您有帮助

package org.test.jaxb;

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "INDIVIDUO")
@XmlType(propOrder= {"name","cpfcnpj","imoveis"})
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Individuo {

    private String name;

    private Date cpfcnpj;

    private Imovie imoveis;

    @XmlElement(name = "NOME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement(name = "CPFCNPJ")
    public Date getCpfcnpj() {
        return cpfcnpj;
    }

    public void setCpfcnpj(Date cpfcnpj) {
        this.cpfcnpj = cpfcnpj;
    }

    @XmlElement(name = "IMOVEIS")
    public Imovie getImoveis() {
        return imoveis;
    }

    public void setImoveis(Imovie imoveis) {
        this.imoveis = imoveis;
    }

}

package org.test.jaxb;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "IMOVEIS")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Imovie {

    private List<String> matriculas;

    public Imovie() {
        this.matriculas = new ArrayList<>();
    }

    @XmlElement(name = "MATRICULA")
    public List<String> getMatriculas() {
        return matriculas;
    }

    public void setMatriculas(List<String> matriculas) {
        this.matriculas = matriculas;
    }

}


package org.test.jaxb;

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

public class DeSerialize {

    private final static String XML_STRING = "<INDIVIDUO>\r\n" + 
            "    <NOME>WANDERLEY JOSE CASSIANO SANT ANNA</NOME>\r\n" + 
            "    <CPFCNPJ>012.266.658-53</CPFCNPJ>\r\n" + 
            "    <IMOVEIS>\r\n" + 
            "        <MATRICULA>1511</MATRICULA>\r\n" + 
            "        <MATRICULA>1512</MATRICULA>\r\n" + 
            "        <MATRICULA>1680</MATRICULA>\r\n" + 
            "        <MATRICULA>4698</MATRICULA>\r\n" + 
            "    </IMOVEIS>\r\n" + 
            "</INDIVIDUO>"; 

    public static void main(String[] args) throws Exception {

        JAXBContext jaxbContext = JAXBContext.newInstance(Individuo.class, Imovie.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); 
        Individuo result = null;
        try ( ByteArrayInputStream in = new ByteArrayInputStream(XML_STRING.getBytes(Charset.defaultCharset())) ) {
            XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(in);
            result = (Individuo) unmarshaller.unmarshal(reader);
        }
        System.out.print("[");
        result.getImoveis().getMatriculas().stream().forEach((String matricula) -> {
            System.out.print(String.format(" %s ", matricula) );
        });
        System.out.println("]");

    }

}