Move the NameSpace declaration to Inner element [message #1821935] |
Sun, 23 February 2020 13:59  |
Eclipse User |
|
|
|
Hello,
I'm using the IBM RQM's webservice for creating Test cases from my application. While sending the data to the server, it follows feed.xsd and qm.xsd. We have created the ECORE model from these XSDs and we have populated various model data from the application. Now, I have to serialize the EMF model to an XML file before sending across to the web service server. I have attached the schemas along with this post.
When I save the EMF model, I see that all the namespaces are saved to the root element (that is feed element)
<?xml version="1.0" encoding="ASCII"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:qm="http://jazz.net/xmlns/alm/qm/v0.1/">
<entry>
<content type="application/xml">
<qm:testcase>
<dc:title>testTitle</dc:title>
<qm:category term="category" value="QM"/>
</qm:testcase>
</content>
</entry>
</feed>
But, the web server accepts the below format only:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<entry xmlns="http://www.w3.org/2005/Atom">
<content type="application/xml">
<qm:testcase xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:qm="http://jazz.net/xmlns/alm/qm/v0.1/">
<dc:title>testTitle</dc:title>
<qm:category term="category" value="QM"/>
</qm:testcase>
</content>
</entry>
</feed>
When I send the first mentioned XML, it throws the below exception:
The uploaded content does not consist of well-formed XML or does not match the XML schema for testcase</ns2:message><ns2:trace>javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: The prefix "qm" for element "qm:testcase" is not bound.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:350)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:593)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:267)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:232)
Below is the code I use for serializing:
Resource xmlResource = new AtomResourceFactoryImpl().createResource(URI.createFileURI(new File("C:/temp/export/testCase.xml").getAbsolutePath()));
xmlResource.getContents().add(atomFeedDocumentModel);
Map<Object, Object> options = new HashMap<>();
xmlResource.save(options);
There is another ResourceFactory named QMResourceFactory. is there a way to generated the XML in such a way that the namespace is defined in the child element?
Attached the ECORE model overview as picture.
Please shed some light over this issue.
Thanks in advance,
Paul
Attachment: feed.xsd
(Size: 4.66KB, Downloaded 99 times)
Attachment: qm.xsd
(Size: 550.77KB, Downloaded 110 times)
Attachment: EMF overview.png
(Size: 22.04KB, Downloaded 83 times)
|
|
|
|
|
|
Re: Move the NameSpace declaration to Inner element [message #1822759 is a reply to message #1821954] |
Fri, 13 March 2020 00:44  |
Eclipse User |
|
|
|
Thanks Ed.
I followed you suggestion I have did the following and it worked. Thank you :)
1) I have overridden the XMLSaveImpl's saveFeatures(EObject o) method, by adding the namespace delcaration, in case of the given object is the testcase:
protected boolean saveFeatures(EObject o) {
if(o instanceof Testcase) {
addNamespaceDeclarations();
}
return super.saveFeatures(o);
}
2) I overridden the createXMLSave() method in AtomResourceImpl and provided the custom created XMLSave object:
protected XMLSave createXMLSave() {
XMLHelper createXMLHelper = createXMLHelper();
createXMLHelper.getPrefix(QmPackage.eINSTANCE);
createXMLHelper.getPrefix(DcPackage.eINSTANCE);
return new SaveQMNsXmlSaveImpl(createXMLHelper);
}
|
|
|
Powered by
FUDForum. Page generated in 0.03712 seconds