我正在尝试反序列化 XML 并为某些属性获取 null
I'm trying to deserialize an XML and getting null for some of the attributes
我正在尝试将下面的 XML 反序列化为对象,但其中一个值(必需)返回 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
我正在使用下面的代码反序列化 eclipse emfutil 反序列化你能告诉我为什么 configuration.getvalue() 返回 null 而不是返回 '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;
}
This post 在 Eclipse 论坛上有一个如何执行此操作的示例,并讨论了它可能无法工作的几个原因。
完整示例如下:
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();
}
我正在尝试将下面的 XML 反序列化为对象,但其中一个值(必需)返回 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
我正在使用下面的代码反序列化 eclipse emfutil 反序列化你能告诉我为什么 configuration.getvalue() 返回 null 而不是返回 '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;
}
This post 在 Eclipse 论坛上有一个如何执行此操作的示例,并讨论了它可能无法工作的几个原因。
完整示例如下:
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();
}