2016-10-10 10 views
1

私はJavaを使用していますが、1つのAutomationMLファイル(XML形式ファイル)から情報を取得する必要があります。私はJAXBを使ってそれをしようとしますが、結局私は必要な情報を得ることができません。 AMLではいくつかの属性を持つ3つのInternalElementを持つ1つのInstanceHierarchyがあり、その属性値が必要ですが、JAXBを使用してAttributeNameを取得しましたが、その値を取得できません。XML形式のファイルの解析

public static void main(String[] args) throws Exception { 

    CAEXFile caex = null; 
    CAEXFile.InstanceHierarchy ih = null; 
    try { 

     JAXBContext jc = JAXBContext.newInstance(CAEXFile.class); 
     //JAXBContext jc = JAXBContext.newInstance(generated.CAEXFile.InstanceHierarchy.class); 
     Unmarshaller ums = jc.createUnmarshaller(); 
     CAEXFile aml = (CAEXFile)ums.unmarshal(new File("src\\teste2.aml")); 

     System.out.println("ins = " + aml.getInstanceHierarchy().get(0).getInternalElement().get(0).getAttribute().get(0).getName()); 

    } catch (JAXBException e) { 
    System.out.println(e.getMessage()); 
    } 
} 

XSDファイルXSD (CAEX)とAMLファイルAML 誰かがJAXBを使用して私を助けたり、私にこれを解決する方法をいくつかの方向性を与えることはできますか? ありがとうございます。

答えて

1

実際にJAXBを避けることができます。これは、コードの残りの部分によっては役に立ちます。おそらくJava 8を使用することができる場合Dynamics &ダイレクトソリューションといいでしょう。

XmlDynamic example = new XmlDynamic(xmlStringOrReaderOrInputSourceEtc); 

String firstInternalName = example.get("CAEXFile|InstanceHierarchy|InternalElement|@Name").asString(); 
// TestProduct_1 

List<String> allInternalNames = example.get("CAEXFile").children() 
    .filter(hasElementName("InstanceHierarchy")) // import static alexh.weak.XmlDynamic.hasElementName; 
    .flatMap(Dynamic::children) 
    .filter(hasElementName("InternalElement")) 
    .map(internalElement -> internalElement.get("@Name").asString()) 
    .collect(toList()); 
// [TestProduct_1, TestResource_1, TestProduct_2, TestProduct_3, TestResource_2] 

これは、単一かつ軽量な余分な依存関係は、のIEのmavenで:返信用

<dependency> 
    <groupId>com.github.alexheretic</groupId> 
    <artifactId>dynamics</artifactId> 
    <version>2.3</version> 
</dependency> 
+0

おかげで、私は二重のアンマーシャリングで解決;) –

関連する問題