问题描述
本着这种精神,我正在升级目前具有XML表示形式的Java对象:
I'm upgrading a Java object that currently has XML representation in this spirit:
<myObjects>
<myObject uid="42" type="someEnum">
<name>Waldo</name>
<description>yada yada</description>
<myElement>some_string</myElement>
...
</myObject>
...
</myObjects>
myElement 是可选的-在Java中可以为null,在XML中可以省略.
如果 myElement 具有实际值,我将添加与仅相关的字段(并且为了与以前的XML兼容,它本身是可选的)
myElement is optional - it can be null in Java / omitted in XML.
I'm adding a field that is only relevant if myElement has an actual value(and to keep compatibility with previous XML, it's optional in itself)
我正试图避免这种情况:
I'm trying to avoid this:
<myElement>some_string</myElement>
<myAttr>foo</myAttr>
并且喜欢这样的东西:
<myElement myAttr="foo">some_string</myElement>
但是现在如何敲打我的头两天要如何注释它.
but banging my head for 2 days now on how to annotate it.
- 我考虑过用 XmlTransient 进行标记,然后让 XmlAnyElement 进行编组时捕获它-但是在将对象从Java编组到Java时,这似乎会引起问题. XML.
- 我尝试为该元素创建一个 XmlAdapter -但是unmarshal方法仅获取内部内容("some_string"). 我是否缺少这种技术?
- 有没有办法将元素作为字符串(< myElement myAttr = \" foo \> some_string</myElement>")获得,我将处理是我自己吗?
- 您是否建议其他方法?
- I thought of marking it with XmlTransient and let an XmlAnyElement catch it instead while unmarshalling - but it seems this will cause a problem when marshalling the object back from Java to XML.
- I tried creating an XmlAdapter for the element - but the unmarshal method gets only the inner content ("some_string"). Am I missing something with this technique?
- Is there a way to just get the element as a string ("<myElement myAttr=\"foo\">some_string</myElement>") and I will process it myself?
- Do you recommend any other approach?
推荐答案
您可以使用 EclipseLink JAXB (MOXy) @XmlPath扩展即可轻松实现:
You can use the EclipseLink JAXB (MOXy) @XmlPath extension to easily accomplish this:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class MyObject {
@XmlAttribute
private int uid;
@XmlAttribute
private String type;
private String name;
private String description;
private String myElement;
@XmlPath("myElement/@myAttr")
private String myAttr;
}
此类将与以下XML交互:
This class will interact with the following XML:
<myObject uid="42" type="someEnum">
<name>Waldo</name>
<description>yada yada</description>
<myElement myAttr="foo">some_string</myElement>
</myObject>
使用以下演示代码:
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyObject.class);
File file = new File("input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyObject myObject = (MyObject) unmarshaller.unmarshal(file);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(myObject, System.out);
}
}
要将MOXy指定为JAXB实现,您需要在与模型类相同的包中包含一个名为jaxb.properties的文件,并带有以下条目:
To specify MOXy as your JAXB implementation you need to include a file called jaxb.properties in the same package as your model classes with the following entry:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
有关基于MOXy的XPath映射的更多信息,请参见:
For more information on MOXy's XPath based mapping see:
- http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
- http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
- http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
- http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
这篇关于注释向元素添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!