Java – XML unmarshalling using JAXB with namespace and schema

I have an XML document as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xs:msgdata xmlns:xs="http://www.myCompany.com" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.myCompany.com msgdata.xsd">
  <xs:msgid>MsgID001</xs:msgid>
  <xs:msgHash>hlkJKLHljkhkjlHKJLHkjl6y987HJKH</xs:msgHash> 
</xs:msgdata>

I was also sent a schema document (called msgdata. XSD) I am using JAXB to ungroup the above XML documents into Java objects The disaggregation code is as follows:

final JAXBContext context = JAXBContext.newInstance(clazz);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("C:\\temp\\msgdata.xsd"));

final Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(schema);

return (T) unmarshaller.unmarshal(new StringReader(xml));

The target object of the XML is as follows (then convert the object to a dormant entity)

@XmlRootElement(name = "msgdata")
public class Message {

    private     String  msgid;
    private     String  msgHash;

    @XmlElement(name = "msgid")
    public String getMsgid() {
        return msgid;
    }

    public void setMsgid(String msgid) {
        this.msgid = msgid;
    }

    @XmlElement(name = "msgHash")
    public String getMsgHash() {
        return msgHash;
    }
    public void setMsgHash(String msgHash) {
        this.msgHash = msgHash;
    }

Several questions:

>I have a rest service and I will receive an XML document in the format shown above What is the reference of the architecture document I know that schema documents are used to validate XML documents I think the way it works is that I use the schema to validate XML documents I receive through rest services The question now is how do I access the architecture? I just store it on my file system and access it, as shown above?

The XML document has a reference to the schema (see the schemalocation entry) How does it find schema documents on my file system? Is this schema reference required in the XML document?

>The current XML document cannot be ungrouped When I try to ungroup, I receive the following error:

I think namespaces are defined in XML documents I either didn't use the schema correctly or the namespace definition was incorrect What on earth did I do wrong?

thank you.

Solution

In general, you can start here. For how to access files (XSD or other) in Java Web Applications (because you mentioned rest) Never rely on absolute path, deployment and security reasons

The XSI schema location attribute is just a hint; They are not necessary Even if it exists, it doesn't have to be considered For server-side applications, I will never use them; One should rely on embedded resources or use the concept of a directory in a trusted / secure location

The fact that it is not ungrouped is clear in the error message: it requires a namespace free element < {} msgdata > instead of <{ http://www.myCompany.com } msgdata>.

You need to modify the XML or modify your class to include such as @ xmlelement (name = "...", namespace)=“ http://www.myCompany.com ”)Something like that

I would say that the given schema and XML are correct, and your comments are out of sync

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>