简体   繁体   English

使用jaxb解析xml字符串

[英]parse xml string using jaxb

I am trying to parse an xml string using jaxb. 我正在尝试使用jaxb解析xml字符串。 In fact , I need to extract the decimal value in literal . 实际上,我需要提取文字中的十进制值。

That's the XML string : 那是XML字符串:

<results>
    <result>
      <binding name="value">
        <literal datatype="http://www.w3.org/2001/XMLSchema#decimal">369.0</literal>
      </binding>
    </result>
  </results>

I have a java class Results : 我有一个java类的结果:

package client;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Results {

    @XmlElement
    String result;

    @XmlElement
    Double binding;

    @XmlElement
    Double literal;
    public Double getLiteral()
    {
        return literal;
    }
    public Double geBinding() 
    {
        return binding;
    }
    public String getResult() 
    {
        return result;
    }

}

When I tried to print the value , I'm getting null .So How can I get the decimal value between literal tag ? 当我尝试打印该值时,我得到的是null。那么如何获取文字标记之间的十进制值?

Results re=(Results) JAXBContext.newInstance(Results.class)
            .createUnmarshaller()
            .unmarshal(new StringReader(my_xml));

System.out.println(re.getLiteral());

Your Results class does not reflect the structure of the XML you're trying to parse. 您的Results类无法反映您要解析的XML的结构。 The results element consists of a result element, which in turn consists of binding and that consists of literal . results元素由一个result元素组成, result元素又由binding组成,并且由literal组成。

To do this through JAXB, we'll have to follow a similar structure. 要通过JAXB做到这一点,我们必须遵循类似的结构。

@XmlRootElement
public class Results {

    @XmlElement
    Result result;

    public Result getResult() {
        return result;
    }
}

public class Result {
    @XmlElement
    Binding binding;

    public Binding getBinding() {
        return binding;
    }
}

public class Binding {
    @XmlElement
    Double literal;

    public Double getLiteral() {
        return literal;
    }
}

To access the value of literal , we can call the getters like results.getResult().getBinding().getLiteral() . 要访问literal的值,我们可以将getter称为results.getResult().getBinding().getLiteral()

However, if this is a one off occurance and your application would not be dealing with XML a lot, you can consider using XPath . 但是,如果这是一次性事件,并且您的应用程序不会大量处理XML,则可以考虑使用XPath

One way to simplify your code is to use MOXy the EclipseLink's JAXB and its XmlPath annotation allowing to provide a XPATH such that you can map directly element or attribute content to your field which allow to avoid having an additional class for each sub element. 简化代码的一种方法是使用MOXyEclipseLink's JAXBXmlPath注释允许提供一个XPATH ,这样你可以直接映射元素或属性内容,你的领域允许以避免对每个子元件的额外类。

For example in your case, the mapping would be: 例如,在您的情况下,映射将为:

@XmlPath("result/binding/literal/text()")
Double literal;

You will need to add this dependency to your project: 您将需要将此依赖项添加到您的项目中:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.6.3</version>
</dependency>

And specify explicitly the context factory to use thanks to a system property to set in your launch command -Djavax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 并通过在启动命令-Djavax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory设置的系统属性来明确指定要使用的上下文工厂。

Here is a good article about MOXy describing how to simplify your code with its features. 这是一篇有关MOXy的好文章,描述了如何使用其功能简化代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM