当前位置: 首页 > 知识库问答 >
问题:

使用JAXB解组xml字符串,但得到空字符串

雍焱
2023-03-14

问题解决

将XmlElementRef注释更改为@XmlElementRef(name=“Option”,type=Option.class,required=false)后,将@XmlRootElement(name=“Option”)添加到选项中。班

更新:

根据laune的建议,我添加了两个类(Options和Option),并修改了GuimopProperties类,如下所示

@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$0(DeviceMgtServiceImpl.java:238)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl$1.load(DeviceMgtServiceImpl.java:233)
at devicemanage.service.appdeploy.DeviceMgtServiceImpl$1.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$$1a89f063.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$1.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$$97274215.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$1.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$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.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解析定义了一个类,如下所示:

@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>

在我的OptionAdapter的解组方法中。类中,第一个选项元素工作正常,有一个字符串“testOptions”输入。但是,第二个one Options元素没有。输入参数将是\n\t\t\t\t,但不是字符串

选择适配器。分类如下

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;
    }
}

任何帮助都很感激。


共有1个答案

倪培
2023-03-14

混合内容并不十分简单,尽管JAXB可以在没有适配器的情况下处理它。

您可以将选项字段的类型定义为选项(而不是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类型的元素


@XmlAccessorType(XmlAccessType.FIELD)
public class Option {
    @XmlAttribute
    protected String name;
    @XmlAttribute
    protected String value;
    //...
}

您必须迭代该列表,以了解您使用的是第一个XML表单还是第二个XML表单。在第一种情况下,只有一个字符串。在第二种情况下,包装的选项对象之间可能存在空白字符串。

 类似资料:
  • 当我使用JAXB时,出现了一些问题。 我将实体转换为xml字符串,一切正常。 但当我将xml字符串转换回实体时,一些信息丢失了(它们都具有相同的类型)。 在实体中:

  • 错误中的SOAP WSDL URL是Spring Boot应用程序中的另一个web服务。试图理解为什么这会出现在错误中。 UnmarshalException:意外元素(URI:“”,local:“message”)。需要的元素是<{http://soap_wsdl_url}jaxb_pojo_name>

  • 问题内容: 想象以下情况:我们从某个外部工具收到一个xml文件。最近,在此xml中,节点名称中或它们的richcontent标记中可能有一些转义的字符,如以下示例所示(简化): 在使用JAXB解组文件后,那些逃脱的字符将不被转义。不幸的是,我需要他们保持原样,这意味着逃脱了。有什么方法可以避免在解组时将这些字符转义? 使用JAXB甚至有可能实现这一目标,还是我们甚至必须考虑更改为其他xml阅读器A

  • 问题内容: 我有一个使用Jaxb进行XML <->转换的应用程序,并使用maven-jaxb2-plugin自动生成了类。 在我模式的某个深处,我可以输入“ ANY” xml。 更新:这更好地描述了我的架构。一些已知的XML包装了一个完全未知的部分(“ any”部分)。 这样(通过jaxb)映射到这样的内部类。 当我解组整个结构时,这没问题。“任何对象”将呈现到org.apache.xerces.

  • 问题内容: 如何使用下面的代码解组XML字符串并将其映射到下面的JAXB对象? 问题答案: 要传递XML内容,您需要将内容包装在中,然后将其解组:

  • 我必须在JAXB中将xml解析为字符串对象。但是如何为这个xml创建对象 Country.xml 为了解析这个xml,我创建了一个类来映射对象,它创建对象并在控制台中打印出来。却不知道自己做错了什么。 当我运行程序时,我会得到 com . sun . XML . internal . bind . v2 . runtime . illegalanotationexception:illegalan