我看到在这里发布了一个类似的问题,但它并没有帮助我解决问题,所以我在这里发布我的问题,看看是否有人可以修改我的代码以使其工作。
问题:如何访问混合内容字符串值并将其保存在 setPhrase(String value) 方法中?
标题.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="link1" xmlns:prefix2="link2" prefix1:att1="att1">
<head>
<styling>
<style prefix1:att1="att1" prefix2:att2="att2" prefix2:att3="att3" prefix2:att4="att4" />
</styling>
<layout />
</head>
<body xmlns:prefix3="link3">
<div prefix1:att1="att1" prefix1:att2="att2">
<prefix3:info att1="att1" att2="att2" />
<p att1="att1" att2="att2" att3="att3">
<prefix3:status att1="att1" att2="att2" />
Hello World.
</p>
</div>
</body>
</tt>
标题.java:
package com;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "p")
@XmlType(propOrder = { "att1", "att2", "att3", "phrase", "subelement"})
public class Caption {
private String att1;
private String att2;
private String att3;
private String phrase;
private Subelement subelement = new Subelement();
@XmlMixed
public void setPhrase(String value)
{
this.phrase = value;
}
public String getPhrase()
{
return phrase;
}
@XmlElementRefs({@XmlElementRef(name = "subelement", type = Subelement.class)})
@XmlMixed
public void setSubelement(Subelement subelement )
{
this.subelement = subelement;
}
public Subelement getSubelement()
{
return subelement;
}
@XmlAttribute
public void setAtt1( String att1 )
{
this.att1 = att1;
}
public String getAtt1()
{
return att1;
}
@XmlAttribute
public void setAtt2( String att2 )
{
this.att2 = att2;
}
public String getAtt2()
{
return att2;
}
@XmlAttribute
public void setAtt3( String att3 )
{
this.att3 = att3;
}
public String getAtt3()
{
return att3;
}
}
在使用 JAXB unmarshall 和 marshall 之后,我能够将所有内容转换为对象并保存 accerling,但实际的短语“Hello World.”除外。我知道我必须对这个复杂的元素使用某种@XmlMixed,但我无法弄清楚。
我当前的 output.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="link1" xmlns:prefix2="link2" prefix1:att1="att1">
<head>
<styling>
<style prefix1:att1="att1" prefix2:att2="att2" prefix2:att3="att3" prefix2:att4="att4" />
</styling>
<layout />
</head>
<body xmlns:prefix3="link3">
<div prefix1:att1="att1" prefix1:att2="att2">
<prefix3:info att1="att1" att2="att2" />
<p att1="att1" att2="att2" att3="att3">
<prefix3:status att1="att1" att2="att2" />
</p>
</div>
</body>
</tt>
愿望输出.xml:(与caption.xml相同)
<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="link1" xmlns:prefix2="link2" prefix1:att1="att1">
<head>
<styling>
<style prefix1:att1="att1" prefix2:att2="att2" prefix2:att3="att3" prefix2:att4="att4" />
</styling>
<layout />
</head>
<body xmlns:prefix3="link3">
<div prefix1:att1="att1" prefix1:att2="att2">
<prefix3:info att1="att1" att2="att2" />
<p att1="att1" att2="att2" att3="att3">
<prefix3:status att1="att1" att2="att2" />
Hello World.
</p>
</div>
</body>
</tt>
提前感谢任何帮助我可以访问此值并将其保存在 setPhrase(String value) 方法中。