Search code examples
javaeclipsexml-deserializationemfecore

I'm trying to deserialize an XML and getting null for some of the attributes


I'm trying to deserialize the below XML to an object, but one of the values (Required)is returning null.

<?xml version="1.0" encoding="UTF-8"?>
-<sy:config xmlns:sy="http://www.example.com/def/sy">
 -<sy:configurations>
  -<sy:configuration property="isReq" name="ABC">
    **Required**
    <atom:link title="ABC Uri" xmlns:atom="http://www.w3.org/2005/Atom" rel="http://www.example.com/def//id" 
    href="abc/bc/def/docid"/>
   </sy:configuration>
 </sy:configurations>
</sy:config>
enter code here

I'm using the below code to deserialize eclipse emfutil to deserialize could you please let me know why the configuration.getvalue() is returning null instead of returning 'Required'

private static <T extends EObject> T readEObjectFromInputStream(InputStream inputStream, String emfFileExtension,Class<T> expectedResultType) throws IOException {
        org.eclipse.emf.common.util.URI emfResourceUri = org.eclipse.emf.common.util.URI
                .createPlatformResourceURI(FILE_PATH + emfFileExtension, true);
        Resource emfResource = new ResourceSetImpl().createResource(emfResourceUri);

        emfResource.load(inputStream, null);
        EObject eObject = emfResource.getContents().get(0);

        T result = expectedResultType.cast(eObject);
        return result;
    }

Solution

  • This post on the Eclipse forums has an example of how to do this, and a discussion of several reasons why it might not be working.

    Here's the full example:

    try {       ResourceSet resourceSet = new ResourceSetImpl();
                Resource resource = resourceSet.createResource(URI.createURI("http:///My.chbasev21"));
                DocumentRoot documentRoot = ChbaseV21Factory.eINSTANCE.createDocumentRoot();
                CompanyDetailsType root = ChbaseV21Factory.eINSTANCE.createCompanyDetailsType();
                documentRoot.setCompanyDetails(root);
                resource.getContents().add(documentRoot);
                //resource.save(Collections.EMPTY_MAP);
                resource.save(System.out, null);
                resource.save(new FileOutputStream("C:/test2.xml"), null);
            }
            catch (IOException exception) {
                exception.printStackTrace();
            }