2015-12-08 41 views
1

我有pojo class变量的返回类型为JAXBElement<String>。我想将它存储在java string.Could中。 有人可以解释如何做到这一点?将JAXBElement <String>值转换为java字符串

File file = new File("C:/Users/Admin/Desktop/JubulaXMLFiles/DemoWithDrools_1.0.xml");  

     JAXBContext jaxbContext = JAXBContext.newInstance(Content.class);  

     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  

     Content e=(Content) jaxbUnmarshaller.unmarshal(file);  
     String retrivedValue = (String)e.getProject().getName().toString(); 
     System.out.println(retrivedValue); 

输出就像[email protected]。但我想检索retrivedValue中的字符串的值。

回答

1

如果getProject()返回类型JAXBElement<String>,则getName()返回XML标记的名称。要获得该元素的值,您需要拨打getValue()

下面为一个小片段

QName qualifiedName = new QName("", "project"); 
JAXBElement<String> project = new JAXBElement<>(qualifiedName, 
     String.class, null, "funnyCoding"); 
System.out.printf("getName() - %s%n", project.getName()); 
System.out.printf("getValue() - %s%n", project.getValue()); 

输出

getName() - project 
getValue() - funnyCoding