【问题标题】:XML Value extraction using Java JAXB使用 Java JAXB 提取 XML 值
【发布时间】:2014-10-16 10:48:41
【问题描述】:

在以下情况下我需要帮助。我有一个 XML 字符串响应。在那个 XML 响应中,我需要提取三个值。我无法实现它。我已经指定了 XML 响应和我需要的值。我还使用 pojo 单行工具创建了一组 .java 文件。

   My XML Response:
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <control>
        <status>success</status>
        <senderid>XXXXX</senderid>
        <controlid>ControlIdHere</controlid>
        <uniqueid>false</uniqueid>
        <dtdversion>3.0</dtdversion>
  </control>
  <operation>
        <authentication>
              <status>success</status>
              <userid>XXXXX1</userid>
              <companyid>XXXXX</companyid>
              <sessiontimestamp>2014-08-22T07:12:37-07:00</sessiontimestamp>
        </authentication>
        <result>
              <status>success</status>
              <function>readByQuery</function>
              <controlid>testControlId</controlid>
              <data listtype="customer" count="100" totalcount="5142" numremaining="5042">
                    <customer>
                     <name>A</name>
         <id>12</id>
                    </customer>
                    <customer>
                     <name>A</name>
         <id>12</id>
                    </customer>
                    <customer>
                     <name>A</name>
         <id>12</id>
                    </customer>
              </data>
        </result>
  </operation>

在这个 XML 响应中,我需要以下值

点赞数=100,总数=5142

我的对象类是这样的

public class Data {
private String totalcount;
private Sodocument[] sodocument;
private String numremaining;
private String count;
private String listtype;
public String getTotalcount ()
{
    return totalcount;
}

public void setTotalcount (String totalcount)
{
    this.totalcount = totalcount;
}

public Sodocument[] getSodocument ()
{
    return sodocument;
}

public void setSodocument (Sodocument[] sodocument)
{
    this.sodocument = sodocument;
}

public String getNumremaining ()
{
    return numremaining;
}

public void setNumremaining (String numremaining)
{
    this.numremaining = numremaining;
}

public String getCount ()
{
    return count;
}
}


  XMLInputFactory xif = XMLInputFactory.newFactory();
  Reader reader = new StringReader(response.toString());
  XMLStreamReader xsr = xif.createXMLStreamReader(reader);
  JAXBContext jc = JAXBContext.newInstance(Data.class);
  Unmarshaller unmarshaller = jc.createUnmarshaller();
  Data jb = unmarshaller.unmarshal(xsr,Data.class).getValue();
  System.out.println(jb.getCount());

这是我的 JAXB 类。对于值 getCount 给了我空响应。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 我相信您的 Data 类需要使用适当的注释(@XmlRootElement、@XmlElement、@XmlAttribute)才能正确解析。如果您的 xml 有一个架构(有一些工具可以为您生成 xsd),那么您可以使用 xjc 生成带有适当注释的 Data 类。
  • 我尝试过@XMLRootElement 和(@XMLElement 和@XMLAttribute),将“数据”作为值。我在这里做错了吗。什么是正确的做法。有了这个,我需要在注释中给出正确的答案

标签: java xml jaxb


【解决方案1】:

生成的类太长,我无法在此处粘贴,但您可以尝试以下方法:

1) 使用 freeformatter.com/xsd-generator.html 从您的 xml 生成 xsd。将生成的 xsd 复制并粘贴到文件中。

2) 使用xjc(自带jdk安装)生成java类。它是一个命令行工具,你只需要运行“xjc generated.xsd”,它就会生成你的类的注释版本。生成的文件将用于整个 xml 响应 - 因此您的类实际上是 Response.java,而不是 Data.java,然后您可以深入了解 Data 元素及其属性。

【讨论】:

  • 有没有其他方法可以不用注释?
  • 如果你想使用 JAXB 来编组/解组你的 xml,你将不得不使用注解。如果您希望 Data.java 保持原样,那么我仍然建议使用 xjc 生成带注释的类,然后编写一个函数将相关数据从 xml 保存到您的 pojo 类(Data)中。
【解决方案2】:

添加这些注释:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Data {
    @XmlAttribute
    private String totalcount;
    @XmlElement
    private Sodocument[] sodocument;
    @XmlAttribute
    private String numremaining;
    @XmlAttribute
    private String count;
    @XmlAttribute
    private String listtype;
    ...

但请注意,Sodocument[] sodocument(应该是一个列表!)与&lt;customer&gt; 不匹配。不确定模型是什么 - 它是否也是另一个元素(取决于列表类型!)

【讨论】:

    猜你喜欢
    • 2015-05-26
    • 2014-06-30
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2019-11-07
    • 1970-01-01
    相关资源
    最近更新 更多