简体   繁体   English

JAXB和继承

[英]JAXB and Inheritance

I have this xsd : 我有这个xsd:

    <?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.org/XsdMath2" 
        xmlns:tns="http://www.example.org/XsdMath2" 
        elementFormDefault="qualified">


    <complexType name="Utilisateur">
        <complexContent>
            <extension base="tns:TraitementDTO">
                <sequence>
                    <element name="nomUtilisateur" type="string"></element>
                    <element name="passUtilisateur" type="string"></element>
                </sequence>
            </extension>
        </complexContent>
    </complexType>

 <complexType name="TraitementDTO">
        <sequence>
            <element name="CodeTraitement" type="int"></element>
        </sequence>
    </complexType>


    <element name="Create" type="tns:TraitementDTO"></element>
</schema>

I would like to know data on my class "Utilisateur" but with my xml : 我想知道我的类“ Utilisateur”上的数据,但是有我的xml:

 <Create xmlns="http://www.example.org/XsdMath2">
       <TraitementDTO>
          <CodeTraitement>0</CodeTraitement>
            <Utilisateur>
               <nomUtilisateur>nomok</nomUtilisateur>
               <passUtilisateur>passok</passUtilisateur>
             </Utilisateur>
       </TraitementDTO>
    </Create>

I can't make (instanceof) my class "TraitementDTO" or i can't use function with my class "Utilisateur", or Cast. 我无法使(实例化)我的类“ TraitementDTO”,或者我无法对我的类“ Utilisateur”或Cast使用函数。 but it's not working. 但它不起作用。 my code java : 我的代码java:

 StringReader sr = new StringReader(this.message);
    JAXBContext context = JAXBContext.newInstance("ActionMathML");
    Unmarshaller decodeur = context.createUnmarshaller();
     msgObject = decodeur.unmarshal(sr);  

         System.out.println(" action" + msgObject);

     if (msgObject instanceof TraitementDTO)
    {
         System.out.println(" action");
    }

How I can take my Data because i try some solutions but it's not working. 我如何获取数据,因为我尝试了一些解决方案但无法正常工作。 My Xsd is correct to retrieve my data ? 我的Xsd是否正确检索我的数据? Regards 问候

Your XML does not match your XML Schema. 您的XML与您的XML模式不匹配。 If you populate your object model and then marshal it you will see what the XML is supposed to look like. 如果填充对象模型然后将其编组,您将看到XML的外观。

Marshal Example 元帅的例子

MarshalDemo MarshalDemo

package ActionMathML;

import javax.xml.bind.*;

public class MarshalDemo {

    public static void main(String[] args) throws Exception {
        JAXBContext context = JAXBContext.newInstance("ActionMathML");

        ObjectFactory objectFactory = new ObjectFactory();

        Utilisateur utilisateur = objectFactory.createUtilisateur();
        utilisateur.setCodeTraitement(0);
        utilisateur.setNomUtilisateur("nomok");
        utilisateur.setPassUtilisateur("passok");

        JAXBElement<TraitementDTO> jaxbElement = objectFactory.createCreate(utilisateur);

        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(jaxbElement, System.out);
    }

}

Output 产量

Below is what the XML should look like based on your XML schema. 以下是基于XML模式的XML外观。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Create xmlns="http://www.example.org/XsdMath2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Utilisateur">
    <CodeTraitement>0</CodeTraitement>
    <nomUtilisateur>nomok</nomUtilisateur>
    <passUtilisateur>passok</passUtilisateur>
</Create>

Unmarshal Example 解组例子

input.xml input.xml中

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Create xmlns="http://www.example.org/XsdMath2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Utilisateur">
    <CodeTraitement>0</CodeTraitement>
    <nomUtilisateur>nomok</nomUtilisateur>
    <passUtilisateur>passok</passUtilisateur>
</Create>

UnmarshalDemo UnmarshalDemo

package ActionMathML;

import java.io.File;
import javax.xml.bind.*;

public class UnmarshalDemo {

    public static void main(String[] args) throws Exception {
        JAXBContext context = JAXBContext.newInstance("ActionMathML");

        Unmarshaller unmarshaller = context.createUnmarshaller();
        File xml = new File("src/ActionMathML/input.xml");
        JAXBElement<TraitementDTO> jaxbElement = (JAXBElement<TraitementDTO>) unmarshaller.unmarshal(xml);

        TraitementDTO traitementDTO = jaxbElement.getValue();
        System.out.println(traitementDTO.getCodeTraitement());

        if(traitementDTO instanceof Utilisateur) {
            Utilisateur utilisateur = (Utilisateur) traitementDTO;
            System.out.println(utilisateur.getNomUtilisateur());
            System.out.println(utilisateur.getPassUtilisateur());
        }
    }

}

Output 产量

0
nomok
passok

For More Information 欲获得更多信息

I have written more about JAXB and inheritance on my blog: 我已经在我的博客上写了有关JAXB和继承的更多信息:

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

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