2
EMF를 사용하여 XSD 기반 액세스 기능을 생성했습니다. 생성 된 예제에서 디스크 파일의 입력을로드하는 방법을 볼 수있었습니다. 그러나 구문 분석하려는 XML은 문자열에 저장됩니다. 문자열을 파일에 덤핑하고 다시 읽지 않고 계속 진행할 수있는 방법이 있습니까?EMF를 사용하여 문자열 형식의 XML 구문 분석
EMF를 사용하여 XSD 기반 액세스 기능을 생성했습니다. 생성 된 예제에서 디스크 파일의 입력을로드하는 방법을 볼 수있었습니다. 그러나 구문 분석하려는 XML은 문자열에 저장됩니다. 문자열을 파일에 덤핑하고 다시 읽지 않고 계속 진행할 수있는 방법이 있습니까?EMF를 사용하여 문자열 형식의 XML 구문 분석
다음은 예제 메서드입니다. modelString과 xml을 파싱하고 EObject를 반환하는 ECorePackage 인스턴스를 가져옵니다.
public static EObject loadEObjectFromString(String myModelXml, EPackage ePackage) throws IOException {
// Create a ResourceSet
ResourceSet resourceSet = new ResourceSetImpl();
// register XMIRegistryResourceFactoryIml
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
(Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMIResourceFactoryImpl());
// register your epackage to the resource set so it has a reference to your ecore
// you can get an instance to your epackage by calling YourEPackageClass.getInstace();
resourceSet.getPackageRegistry().put(ePackage.getNsURI(), ePackage);
Resource resource = resourceSet.createResource(URI.createURI("*.modelextension"));
resource.load(new URIConverter.ReadableInputStream(myModelXml), null);
// return the root model object and there you have it, all you need is to
// cast it to the right EObject based on your model
return resource.getContents().get(0);
}