2012-01-14 32 views
1

首先,我是Jaxb的新手,並且最挑釁的方式並沒有完全圍繞它。JaxB註解驅動提取

我想使用註釋來削減Xml,而不是生成綁定類 我只想提取一部分xml並將其作爲Java.lang.String返回。 不需要編組或解組需求。只是想從xml中提取。

比方說,我的XML的結構爲遵循

<root> 
    <topMovies> 
    <string>The Godfather (1972)</string> 
    <string>The Shawshank Redemption (1994)</string> 
    <string>The Godfather: Part II (1974)</string> 
    <string>The Lord of the Rings: The Return of the King (2003)</string> 
    <topMovies> 
<root> 

而且我想提取上面的XML片段(topMovies)。

這當然不起作用,但可能會給出我想說的內容。

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name = "root") 
public class GetTopMovies { 
@XmlElement(name = "topMovies") 
    protected java.lang.String getTopMovies; 
} 

在我想提取XML Java字符串結束時(不考慮 逸入帳戶)。

String topMovies = 
"<topMovies> 
    <string>The Godfather (1972)</string> 
    <string>The Shawshank Redemption (1994)</string> 
    <string>The Godfather: Part II (1974)</string> 
    <string>The Lord of the Rings: The Return of the King (2003)</string> 
    <topMovies> 
"; 

這可以使用JAXBElement或XmlAdapter或創建一個特殊的對象工廠?

+0

可能重複的[JAXB取元件的所有內容(包括標籤和文本)](http://stackoverflow.com/questions/7736678/jaxb-take -all-content-of-element-both-tags-and-text) – 2012-01-14 20:21:36

回答