使用 JAXB 解组 xml 字符串但得到空字符串
Using JAXB to unmarshal a xml string but get empty one
问题已解决
将 XmlElementRef 注释更改为 @XmlElementRef(name = "Option", type = Option.class, required = false) 并将 @XmlRootElement(name = "Option") 添加到 Option.class
更新:
我添加了两个 classes(选项和选项)作为 laune 的推荐并修改了 GuiMOProperties class 如下
@XmlAccessorType(XmlAccessType.FIELD)
public class GuiMOProperties
{
@XmlElement(name="Grouping")
private boolean grouping;
@XmlElement(name="Type")
private String type;
@XmlElement(name="Regex")
private String regex;
@XmlElement(name="DisplayName")
private String displayName;
@XmlElement(name="Options")
private Options options;
@XmlElement(name="Mask")
private String mask;
@XmlElement(name="DefaultValue")
private String defaultValue;
}
但是当我必须按如下所示新建 JAXBContext 实例时遇到一些异常:
JAXBContext jaxbContext = JAXBContext.newInstance(GuiMOProperties.class);
java.lang.IllegalArgumentException: can't parse argument number: ''{0}''
at java.text.MessageFormat.makeFormat(MessageFormat.java:1429)
at java.text.MessageFormat.applyPattern(MessageFormat.java:479)
at java.text.MessageFormat.<init>(MessageFormat.java:362)
at java.text.MessageFormat.format(MessageFormat.java:840)
at com.sun.xml.internal.bind.v2.model.impl.Messages.format(Messages.java:117)
at com.sun.xml.internal.bind.v2.model.impl.ReferencePropertyInfoImpl.calcTypes(ReferencePropertyInfoImpl.java:171)
at com.sun.xml.internal.bind.v2.model.impl.ReferencePropertyInfoImpl.link(ReferencePropertyInfoImpl.java:372)
at com.sun.xml.internal.bind.v2.model.impl.ClassInfoImpl.link(ClassInfoImpl.java:1257)
at com.sun.xml.internal.bind.v2.model.impl.RuntimeClassInfoImpl.link(RuntimeClassInfoImpl.java:182)
at com.sun.xml.internal.bind.v2.model.impl.ModelBuilder.link(ModelBuilder.java:439)
at com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder.link(RuntimeModelBuilder.java:118)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:443)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at devicemanage.utility.Tree2xml.omElementToGuiMOPropertiesList(Tree2xml.java:332)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.getMOProperties(DeviceMgtServiceImpl.java:250)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.access[=12=](DeviceMgtServiceImpl.java:238)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.load(DeviceMgtServiceImpl.java:233)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.load(DeviceMgtServiceImpl.java:1)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.findMOPropertiesByDeviceType(DeviceMgtServiceImpl.java:208)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl$$FastClassBySpringCGLIB$a89f063.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:266)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl$$EnhancerBySpringCGLIB$274215.findMOPropertiesByDeviceType(<generated>)
at TestDeviceMgtService.testFindMOPropertiesByDeviceType(TestDeviceMgtService.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:81)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NumberFormatException: For input string: "''{0}''"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at java.text.MessageFormat.makeFormat(MessageFormat.java:1427)
... 78 more
我为 JAXB 解析定义了一个 class:
@XmlAccessorType(XmlAccessType.FIELD)
public class GuiMOProperties
{
@XmlElement(name="Grouping")
private boolean grouping;
@XmlElement(name="Type")
private String type;
@XmlElement(name="Regex")
private String regex;
@XmlElement(name="DisplayName")
private String displayName;
@XmlElement(name="Options")
@XmlJavaTypeAdapter(OptionsAdapter.class)
private GuiMOOption options;
@XmlElement(name="Mask")
private String mask;
@XmlElement(name="DefaultValue")
private String defaultValue;
}
我必须按如下方式解析 xml 字符串:
<VolumeUpKey>
<Type/>
<Regex/>
<DisplayName/>
<Options>testOptions</Options>
<Mask/>
<DefaultValue/>
</VolumeUpKey>
<SSID>
<Type/>
<Regex/>
<DisplayName/>
<Options>
<Option name="1" value="1"></Option>
<Option name="2" value="2"></Option>
<Option name="3" value="3"></Option>
<Option name="4" value="4"></Option>
</Options>
<Mask/>
<DefaultValue/>
</SSID>
在我的 OptionsAdapter.class 的解组方法中,第一个选项元素工作正常,有一个字符串 "testOptions" 输入。但是,第二个 Options 元素没有。输入参数将是 \n\t\t\t\t,但不是字符串 <Option name="1" value="1"></Option><Option name="2" value="2"></Option><Option name="3" value="3"></Option><Option name="4" value="4"></Option>
.
OptionsAdapter.class如下
public class OptionsAdapter extends XmlAdapter<String, GuiMOOption>
{
@Override
public GuiMOOption unmarshal(String v) throws Exception
{
try
{
System.out.println("unmarshal string : " + v);
} catch (Exception e)
{
// TODO: handle exception
}
return null;
}
@Override
public String marshal(GuiMOOption v) throws Exception
{
// TODO Auto-generated method stub
return null;
}
}
感谢任何帮助。
混合内容不是很简单,尽管 JAXB 可以在没有适配器的情况下处理它。
您可以将选项字段的类型定义为 Options(而不是 GuiMOOption):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Options", propOrder = {
"content"
})
public class Options {
@XmlElementRef(name = "field", type = JAXBElement.class, required = false)
@XmlMixed
protected List content;
public List getContent() {
if (content == null) {
content = new ArrayList();
}
return this.content;
}
}
列表将包含 JAXBElement<Option>
和字符串类型的元素,其中 Option 是 class
@XmlAccessorType(XmlAccessType.FIELD)
public class Option {
@XmlAttribute
protected String name;
@XmlAttribute
protected String value;
//...
}
您必须迭代列表以了解您是否具有第一种或第二种 XML 形式。在第一种情况下,只有一个字符串。第二种情况,包裹的Option对象中可能有白色的space字符串
问题已解决
将 XmlElementRef 注释更改为 @XmlElementRef(name = "Option", type = Option.class, required = false) 并将 @XmlRootElement(name = "Option") 添加到 Option.class
更新:
我添加了两个 classes(选项和选项)作为 laune 的推荐并修改了 GuiMOProperties class 如下
@XmlAccessorType(XmlAccessType.FIELD)
public class GuiMOProperties
{
@XmlElement(name="Grouping")
private boolean grouping;
@XmlElement(name="Type")
private String type;
@XmlElement(name="Regex")
private String regex;
@XmlElement(name="DisplayName")
private String displayName;
@XmlElement(name="Options")
private Options options;
@XmlElement(name="Mask")
private String mask;
@XmlElement(name="DefaultValue")
private String defaultValue;
}
但是当我必须按如下所示新建 JAXBContext 实例时遇到一些异常:
JAXBContext jaxbContext = JAXBContext.newInstance(GuiMOProperties.class);
java.lang.IllegalArgumentException: can't parse argument number: ''{0}''
at java.text.MessageFormat.makeFormat(MessageFormat.java:1429)
at java.text.MessageFormat.applyPattern(MessageFormat.java:479)
at java.text.MessageFormat.<init>(MessageFormat.java:362)
at java.text.MessageFormat.format(MessageFormat.java:840)
at com.sun.xml.internal.bind.v2.model.impl.Messages.format(Messages.java:117)
at com.sun.xml.internal.bind.v2.model.impl.ReferencePropertyInfoImpl.calcTypes(ReferencePropertyInfoImpl.java:171)
at com.sun.xml.internal.bind.v2.model.impl.ReferencePropertyInfoImpl.link(ReferencePropertyInfoImpl.java:372)
at com.sun.xml.internal.bind.v2.model.impl.ClassInfoImpl.link(ClassInfoImpl.java:1257)
at com.sun.xml.internal.bind.v2.model.impl.RuntimeClassInfoImpl.link(RuntimeClassInfoImpl.java:182)
at com.sun.xml.internal.bind.v2.model.impl.ModelBuilder.link(ModelBuilder.java:439)
at com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder.link(RuntimeModelBuilder.java:118)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:443)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at devicemanage.utility.Tree2xml.omElementToGuiMOPropertiesList(Tree2xml.java:332)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.getMOProperties(DeviceMgtServiceImpl.java:250)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.access[=12=](DeviceMgtServiceImpl.java:238)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.load(DeviceMgtServiceImpl.java:233)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.load(DeviceMgtServiceImpl.java:1)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl.findMOPropertiesByDeviceType(DeviceMgtServiceImpl.java:208)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl$$FastClassBySpringCGLIB$a89f063.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:266)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl$$EnhancerBySpringCGLIB$274215.findMOPropertiesByDeviceType(<generated>)
at TestDeviceMgtService.testFindMOPropertiesByDeviceType(TestDeviceMgtService.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:81)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NumberFormatException: For input string: "''{0}''"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at java.text.MessageFormat.makeFormat(MessageFormat.java:1427)
... 78 more
我为 JAXB 解析定义了一个 class:
@XmlAccessorType(XmlAccessType.FIELD)
public class GuiMOProperties
{
@XmlElement(name="Grouping")
private boolean grouping;
@XmlElement(name="Type")
private String type;
@XmlElement(name="Regex")
private String regex;
@XmlElement(name="DisplayName")
private String displayName;
@XmlElement(name="Options")
@XmlJavaTypeAdapter(OptionsAdapter.class)
private GuiMOOption options;
@XmlElement(name="Mask")
private String mask;
@XmlElement(name="DefaultValue")
private String defaultValue;
}
我必须按如下方式解析 xml 字符串:
<VolumeUpKey>
<Type/>
<Regex/>
<DisplayName/>
<Options>testOptions</Options>
<Mask/>
<DefaultValue/>
</VolumeUpKey>
<SSID>
<Type/>
<Regex/>
<DisplayName/>
<Options>
<Option name="1" value="1"></Option>
<Option name="2" value="2"></Option>
<Option name="3" value="3"></Option>
<Option name="4" value="4"></Option>
</Options>
<Mask/>
<DefaultValue/>
</SSID>
在我的 OptionsAdapter.class 的解组方法中,第一个选项元素工作正常,有一个字符串 "testOptions" 输入。但是,第二个 Options 元素没有。输入参数将是 \n\t\t\t\t,但不是字符串 <Option name="1" value="1"></Option><Option name="2" value="2"></Option><Option name="3" value="3"></Option><Option name="4" value="4"></Option>
.
OptionsAdapter.class如下
public class OptionsAdapter extends XmlAdapter<String, GuiMOOption>
{
@Override
public GuiMOOption unmarshal(String v) throws Exception
{
try
{
System.out.println("unmarshal string : " + v);
} catch (Exception e)
{
// TODO: handle exception
}
return null;
}
@Override
public String marshal(GuiMOOption v) throws Exception
{
// TODO Auto-generated method stub
return null;
}
}
感谢任何帮助。
混合内容不是很简单,尽管 JAXB 可以在没有适配器的情况下处理它。
您可以将选项字段的类型定义为 Options(而不是 GuiMOOption):
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Options", propOrder = { "content" }) public class Options { @XmlElementRef(name = "field", type = JAXBElement.class, required = false) @XmlMixed protected List content; public List getContent() { if (content == null) { content = new ArrayList(); } return this.content; } }
列表将包含 JAXBElement<Option>
和字符串类型的元素,其中 Option 是 class
@XmlAccessorType(XmlAccessType.FIELD) public class Option { @XmlAttribute protected String name; @XmlAttribute protected String value; //... }
您必须迭代列表以了解您是否具有第一种或第二种 XML 形式。在第一种情况下,只有一个字符串。第二种情况,包裹的Option对象中可能有白色的space字符串