JAVA-将XML文档解组到对象
我的问题和以前问过的其他问题类似,但事实是我还不能解决我的问题。 我有一个XML文档,需要将其转换(解组)为对象,我正在使用JAXB注释进行转换。到目前为止,还不错,但有一个属性我无法从中获得价值。 让我编码而不是说话 我的Java对象:JAVA-将XML文档解组到对象,java,xml,jaxb,Java,Xml,Jaxb,我的问题和以前问过的其他问题类似,但事实是我还不能解决我的问题。 我有一个XML文档,需要将其转换(解组)为对象,我正在使用JAXB注释进行转换。到目前为止,还不错,但有一个属性我无法从中获得价值。 让我编码而不是说话 我的Java对象: @XmlRootElement public class Product { private String date_upd; private MetaDescription meta_description; //---------
@XmlRootElement
public class Product {
private String date_upd;
private MetaDescription meta_description;
//------------Static classes for sub nodes------------------
@XmlAccessorType(XmlAccessType.FIELD)
static class MetaDescription{
private List<Language> language ;
}
@XmlAccessorType(XmlAccessType.FIELD)
static class Language{
@XmlAttribute(name="id")
private String id;
@XmlValue
private String language = null;
void setLanguage(String language){
this.language = language;
}
String getLanguage(){
return this.language;
}
}
public String getDate_upd() {
return date_upd;
}
/**
* @param date_upd the date_upd to set
*/
@XmlElement
public void setDate_upd(String date_upd) {
this.date_upd = date_upd;
}
/**
* @return the meta_description
*/
public MetaDescription getMeta_description() {
return meta_description;
}
/**
* @param meta_description the meta_description to set
*/
@XmlElement
public void setMeta_description(MetaDescription meta_description) {
this.meta_description = meta_description;
}
}
@XmlRootElement
公共类产品{
私有字符串日期\ upd;
私有元描述元描述;
//------------子节点的静态类------------------
@XmlAccessorType(XmlAccessType.FIELD)
静态类元描述{
私有列表语言;
}
@XmlAccessorType(XmlAccessType.FIELD)
静态类语言{
@xmltattribute(name=“id”)
私有字符串id;
@XmlValue
私有字符串语言=null;
void setLanguage(字符串语言){
这种语言=语言;
}
字符串getLanguage(){
返回此.language;
}
}
公共字符串getDate_upd(){
返回日期(upd);
}
/**
*@param date\u upd要设置的日期
*/
@XmlElement
公共无效设置日期(字符串日期){
this.date\u upd=date\u upd;
}
/**
*@返回元描述
*/
公共元描述getMeta_描述(){
返回元描述;
}
/**
*@param meta_description要设置的meta_说明
*/
@XmlElement
公共无效集合元描述(元描述元描述){
this.meta_description=meta_description;
}
}
我的XML文档包含以下片段:
<product>
<date_upd>
<![CDATA[2013-12-06 18:03:59]]>
</date_upd>
<meta_description>
<language id="1" xlink:href="http://demo1.it2care.com/shop/api/languages/1">
<![CDATA[product1]]>
</language>
<language id="2" xlink:href="http://demo1.it2care.com/shop/api/languages/2">
<![CDATA[produto1]]>
</language>
</meta_description>
</product>
//Create a Document factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Line I added
factory.setCoalescing(true); //remove all CDATA wrappers
它有更多的财产,但只有这些是相关的date_upd'被正确地解组,但我无法为'meta_description'解决它。我在“MetaDescription”字段中获得了“Language”对象的列表,但是我无法获得“Language”的值,尽管我从“Language”节点获得了“id”属性。
对于所展示的XML示例,“meta_description”是一个包含2个元素的列表。两个列表元素上的“ID”都已正确填充,但我从“语言”中获得了“ID”,而不是“product1”
有什么提示吗?事先非常感谢。我想我明白了!我得到了“”作为语言,因为我的节点中有CDATA元素。出于某种原因,我仍在试图弄清楚,解组器没有进入这些节点,因此返回一个空字符串。 我从源头上解决了这个问题。将XML字符串从服务器转换为XML文档时,我添加了以下片段:
<product>
<date_upd>
<![CDATA[2013-12-06 18:03:59]]>
</date_upd>
<meta_description>
<language id="1" xlink:href="http://demo1.it2care.com/shop/api/languages/1">
<![CDATA[product1]]>
</language>
<language id="2" xlink:href="http://demo1.it2care.com/shop/api/languages/2">
<![CDATA[produto1]]>
</language>
</meta_description>
</product>
//Create a Document factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Line I added
factory.setCoalescing(true); //remove all CDATA wrappers
这解决了我的问题,使用我最初发布的结构。
希望这有帮助