Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Move the NameSpace declaration to Inner element(EMF Serialization)
Move the NameSpace declaration to Inner element [message #1821935] Sun, 23 February 2020 18:59 Go to next message
Palraj Jayaraj is currently offline Palraj JayarajFriend
Messages: 15
Registered: June 2015
Junior Member
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 25 times)
  • Attachment: qm.xsd
    (Size: 550.77KB, Downloaded 21 times)
  • Attachment: EMF overview.png
    (Size: 22.04KB, Downloaded 22 times)
Re: Move the NameSpace declaration to Inner element [message #1821943 is a reply to message #1821935] Mon, 24 February 2020 09:03 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31825
Registered: July 2009
Senior Member
Both forms are valid XML but it looks like the service is not extracting the things it's unmashalling correctly, i.e., from the description it appears that the service is extracting just the test case element and processing that in isolation of the context where the namespace prefixes are defined. That seems rather bogus to me.

In any case, xmlns declarations can only appear on the root element and are only recorded on the root element.



Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Move the NameSpace declaration to Inner element [message #1821948 is a reply to message #1821943] Mon, 24 February 2020 11:48 Go to previous messageGo to next message
Palraj Jayaraj is currently offline Palraj JayarajFriend
Messages: 15
Registered: June 2015
Junior Member
Thank you Ed for the reply.
Yeah, you are right, It seems like the RQM web service is trying to be extract the 'testcase' contents and then, try to resolve the name space.

Is there a way to create the XML file as described?
I'm thinking of using the QMResourceFactoryImpl to create the qm:testcase contents, so that it contains the namespace declaration in testcase's tag. and use the AtomResourceFactoryImpl to create the feed element till content and somehow insert the created qm:testcase inside the content tag?

is there any alternative?

Thanks in advance,
Paul
Re: Move the NameSpace declaration to Inner element [message #1821954 is a reply to message #1821948] Mon, 24 February 2020 13:51 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31825
Registered: July 2009
Senior Member
You might try to specialize org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElement(EObject, EStructuralFeature) such that when it's saving the content element's type attribute that you also include "redundant" xmlsprefix declarations. Set a break point there to see how you arrive there and what classes you need to specialize to override the method.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Move the NameSpace declaration to Inner element [message #1822759 is a reply to message #1821954] Fri, 13 March 2020 04:44 Go to previous message
Palraj Jayaraj is currently offline Palraj JayarajFriend
Messages: 15
Registered: June 2015
Junior Member
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);
  }
Previous Topic:Ecore data types for array, vector and list
Next Topic:How to generate "@since" tags in JDoc?
Goto Forum:
  


Current Time: Sat Apr 10 23:08:00 GMT 2021

Powered by FUDForum. Page generated in 0.02499 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top