2

I have a SOAP message made in C# through SOAP Formatter:

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <a1:Artifact id="ref-1" xmlns:a1="artifactNamespace">
            <code id="ref-4">TestCode</code>        
            <exam href="#ref-14"/>          
        </a1:CrfArtifact>
        <a4:Exam id="ref-14" xmlns:a4="examNamespace">
            <name id="ref-20">TestExam</name>
        </a4:Exam>  
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I'm trying to create correct Java strongly typed obect which would follow this message. In C# I have hierarhy:

Artifact 
 |
 ------Exam

I want to have the same in Java. As you see in SOAP message formatter places Artifact and Exam on the same level and links them through href->ref relation. My Question is how to create such data model which could properly deserialize this message into Java objects with mentioned hierarchy.

My Envelope:

@XmlRootElement(name="Envelope")
@XmlAccessorType(XmlAccessType.FIELD)
public class Envelope {
    @XmlElement(name="Body")
    public Body body;
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Body {
   @XmlElement(name="Artifact")
   public generated.Artifact artifact;
}

My Java data classes:

@XmlAccessorType(XmlAccessType.FIELD)
public class Artifact {

    @XmlElement(name="code")
    public String examcode;    

    @XmlElement(name="exam")
    public Exam exam; 
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Exam {

    @XmlAttribute(name="id")
    @XmlID
    public String examid;

    @XmlElement(name="name")
    public String examname;
}

I used NamespaceFilter to get rid of namespaces to make things more clear, by my Exam node is not picked up by unmarshaller and is always null:

public static void Unmarshal() throws JAXBException, SAXException, FileNotFoundException {

    //Create context for my message
    JAXBContext jaxbContext = JAXBContext.newInstance(Envelope.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    // Create an XMLReader to use the namespace filter
    XMLReader reader = XMLReaderFactory.createXMLReader();

    // Create the filter (to add namespace) and set the xmlReader as its
    // parent.
    NamespaceFilter inFilter = new NamespaceFilter(null, false);
    inFilter.setParent(reader);

    // Prepare the input
    InputSource is = new InputSource(new FileInputStream("src/main/resources/input.xml"));

    // Create a SAXSource specifying the filter
    SAXSource source = new SAXSource(inFilter, is);

    // Do unmarshalling
    Envelope myJaxbObject = (Envelope) jaxbUnmarshaller.unmarshal(source);
}

The result:

enter image description here

Any help is apritiate. Thanks

Alex
  • 8,827
  • 3
  • 42
  • 58

0 Answers0