简体   繁体   English

读取Java中的xml文件-仅选定元素

[英]Reading xml file in Java - only selected elements

I have just started to learn how to make a java object from a XML. 我刚刚开始学习如何从XML制作Java对象。 However, now I have a tricky input and I don't really know how to solve it. 但是,现在我的输入很棘手,我真的不知道如何解决。

This is the input: 这是输入:

<?xml version="1.0" encoding="UTF-8"?>
<return>
  <productChange cd_product="711" ds_product="MBL40337 I151 BLACK P" cd_species="UN" cd_tipi="61062000" cd_cst="5" qt_weight="" cd_nbm="" dt="2016-05-04 11:47:36">
    <productvalue cd_company="1" cd_product="711" tp_value="P" cd_value="1" vl_product="61.39">
      <tipovaluePrd tp_value="P" cd_value="1" ds_value="SALE" cd_corrency="1" />
    </productvalue>
    <productvalue cd_company="1" cd_product="711" tp_value="P" cd_value="4" vl_product="129.8">
      <tipovaluePrd tp_value="P" cd_value="4" ds_value="SALE STORE" cd_corrency="1" />
    </productvalue>
    <productvalue cd_company="1" cd_product="711" tp_value="P" cd_value="5" vl_product="64.9">
      <tipovaluePrd tp_value="P" cd_value="5" ds_value="SALE AT" cd_corrency="1" />
    </productvalue>
    <productvalue cd_company="1" cd_product="711" tp_value="P" cd_value="8" vl_product="122.78">
      <tipovaluePrd tp_value="P" cd_value="8" ds_value="SALE FQ" cd_corrency="1" />
    </productvalue>
  </productChange>
</return>

To consume this xml with JaxB (XML provided by a third party company) I have built the following classes: 为了使用JaxB(第三方公司提供的XML)使用此xml,我构建了以下类:

@XmlAccessorType(XmlAccessType.FIELD)
public class ProductUpdateResponse {

  @XmlAttribute(name = "cd_product")
  private Integer productCode;
  @XmlAttribute(name = "ds_product")
  private String productDescription;
  @XmlAttribute(name = "cd_species")
  private String productSpecie;
  @XmlAttribute(name = "cd_tipi")
  private Integer productTIPI;
  @XmlAttribute(name = "cd_cst")
  private Integer productCST;
  @XmlAttribute(name = "qt_weight")
  private String productWeight;
  @XmlAttribute(name = "cd_nbm")
  private String productNBM;
  @XmlAttribute(name = "dt")
  private String productDate;

  @XmlElement(name = "productvalue")
  private ProductValueType productValue;
// getters and setters
}

@XmlAccessorType(XmlAccessType.FIELD)
public class ProductValue {
  @XmlAttribute(name = "cd_product")
  private Integer productCode;
  @XmlAttribute(name = "cd_company")
  private Integer companyCode;
  @XmlAttribute(name = "tp_value")
  private String valueType;
  @XmlAttribute(name = "cd_value")
  private Integer valueCode;
  @XmlAttribute(name = "vl_product")
  private BigDecimal productValue;

  @XmlElement(name = "tipovaluePrd")
  private ProductValueType productValueType;
//getters and seters
}

@XmlAccessorType(XmlAccessType.FIELD)
public class ProductValueType {
  @XmlAttribute(name = "tp_value")
  private String valueType;
  @XmlAttribute(name = "cd_value")
  private String valueCode;
  @XmlAttribute(name = "ds_value")
  private String valueDescription;
  @XmlAttribute(name = "cd_corrency")
  private Integer valueCurrency;
//getters and seters
}

However, the main information, for me, is comming as null (productvalue) I wonder if it is because there are four tags productvalue in the xml (I just need the one whose tipovaluePrd.cd_value is 4 ). 但是,对我来说,主要信息是空值(productvalue),我想知道是否是因为xml中有四个标记productvalue(我只需要一个tipovaluePrd.cd_value4tipovaluePrd.cd_value )。

Below is the method I'm using to parse the XML: 下面是我用来解析XML的方法:

private ProductUpdateResponse buildResponse(String rawResponse, Class<T> responseClass) {

    JAXBContext jaxbContext = null;
    try {
      jaxbContext = JAXBContext.newInstance(responseClass);
      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
      StringReader reader = new StringReader(rawResponse);
      return (ProductUpdateResponse) unmarshaller.unmarshal(reader);
    } catch (JAXBException | ClassCastException e) {
      return null;
    }
  }

So, I wonder if there is a way to read specifically the value I want ( productvalue element that has tipovaluePrd.cd_value = 4 ) and how could I do that. 因此,我想知道是否有一种方法可以专门读取我想要的值(具有tipovaluePrd.cd_value = 4 productvalue元素)以及如何做到这一点。 If there isn't, how should I build my class so I can read properly everything? 如果没有,应该如何构建我的课程,以便我可以正确阅读所有内容?

Changing the input is not an option here... 在这里不能更改输入...

Thanks in advance for any help. 在此先感谢您的帮助。

I'm actually surprised you're getting a result from JAXB with your current setup. 实际上,您使用当前的设置从JAXB中得到了结果,实在感到惊讶。

You lack a class representing your root element : 您缺少一个代表您的根元素的类:

@XmlRootElement(name="return")
@XmlAccessorType(XmlAccessType.FIELD)
public class Return {
     @XmlElement(name="productchange")
     private ProductUpdateResponse response;

     //getters and setters
}

Also, in your ProductUpdateResponse class, you should change : 另外,在ProductUpdateResponse类中,您应该更改:

@XmlElement(name = "productvalue")
private ProductValueType productValue;

by 通过

@XmlElement(name = "productvalue")
private List<ProductValue> productValues;

Finally, in your buildResponse method, make sure the responseClass argument is Return.class. 最后,在您的buildResponse方法中,确保responseClass参数为Return.class。

With this, you should have a list with all your productvalue element. 这样,您应该有一个包含所有productvalue元素的列表。 You just have to get the productvalue your interested in from it. 您只需要从中获取您感兴趣的产品价值。

If you only want to get the element you're interested with : 如果只想获取您感兴趣的元素:

  1. As swasa suggested it, you can use the javax.xml.xpath to make an Xpath request on your XML. 正如swasa所建议的那样,您可以使用javax.xml.xpath在XML上发出Xpath请求。

  2. If you're willing to change your JAXB Implementation : EclipseLink JAXB Implementation implements an @XmlPath annotation that allows you to bind objects according to an XPath request : 如果您愿意更改JAXB实现:EclipseLink JAXB实现实现@XmlPath批注,该批注允许您根据XPath请求绑定对象:

     @XmlPath(name = "productvalue/tipovaluePrd[@cd_value=4]") @XmlPath(name =“ productvalue / tipovaluePrd [@ cd_value = 4]”)\nprivate ProductValueType productValueType; 私有ProductValueType productValueType; 

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

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