XStream - 解析嵌套元素(跳过父级)

时间:2015-08-18 17:20:14

标签: java xml xml-parsing xstream

我有以下XML,我正在尝试解析w / XStream。问题是,一切正常,直至达到<Keywords>...</Keywords>元素。

我想获得所有<KEYWORD>元素,而无需创建一个类来处理<KEYWORDS>

sample.xml中

<CATALOG>
  <PRODUCTS>
    <PRODUCT>
      <PRODUCTOID>123</PRODUTOID>
      <PRODUCTNAME>TEST NAME</NOMEPRODUTO>
      <DESCRIPTION>TEST DESCRIPTION</DESCRIPTION>
      <KEYWORDS>
        <KEYWORD>keyword_1</KEYWORD>
        <KEYWORD>keyword_2</KEYWORD>
        <KEYWORD>keyword_3</KEYWORD>
        <KEYWORD>keyword_n</KEYWORD>
      <KEYWORDS>
    </PRODUCT>
  </PRODUCTS>
</CATALOG>

Catalog.java

@XStreamAlias("CATALOG")
public class Catalog {

    @XStreamAlias("PRODUCTS")
    private List<Product> products;

   // Getters and Setters

}

Products.java

@XStreamAlias("PRODUCT")
public class Product {

    @XStreamAlias("PRODUCTID")
    private String id;

    @XStreamAlias("PRODUCTNAME")
    private String name;

    @XStreamAlias("DESCRIPTION")
    private String description;


   // @XStreamImplicit(itemFieldName = "KEYWORD")
   // the above line works, *IF* I delete the parent node `<KEYWORDS>`,
   // but that's not the case, I have to deal with it  

   // @StreamAlias("KEYWORDS/KEYWORD")
   // this doesn't work

   @StreamAlias("KEYWORDS")
   // and this returns the error I'm posting below after this class
   private List<String> keywords;

   // Getters and Setters

}

XStreamTest.java

public class XStreamTest {

    public static void main(String[] args) {

    String filepath = "Sample.xml";

    try {
        FileReader reader = new FileReader(filepath);
        XStream xstream = new XStream();

        xstream.processAnnotations(Catalog.class);
        xstream.processAnnotations(Product.class);

        Catalog catalog = (Catalog) xstream.fromXML(reader);
        System.out.println(catalog.getProducts().get(0).getKeywords().get(0));
    }

    catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

使用当前的设置,这就是我得到的(我知道为什么我得到这个...我没有将元素<KEYWORD>映射到任何地方。

但我真的没有想要创建另一个类,说“Keyword.java”只是为了处理<KEYWORDS>(就像我在管理{{1}中的元素一样元素。

无论如何我可以解决这个问题(不必另外上课)?

<PRODUCT>

提前致谢!

2 个答案:

答案 0 :(得分:0)

如果你现在有“KEYWORD”的字符串,我猜你的XML会被正确解析。所以我想你的问题已在这里得到解答:aliasing-contents-of-a-set-using-xstream

您可以尝试XStreamAliasType使用注释执行此操作。

答案 1 :(得分:0)

我在两个方面改变了你的代码:

  • 我添加了&#39; /&#39;到<KEYWORDS>
  • 的结束标记
  • 我添加了一个调用xstream.alias()的映射&#34; KEYWORD&#34;到String类

输出&#34; keyword_1&#34;对我来说:

public static void main(String[] args) 
{
    String xml = 
        "<CATALOG>\n"+
        "  <PRODUCTS>\n"+
        "    <PRODUCT>\n"+
        "      <PRODUCTID>123</PRODUCTID>\n"+
        "      <PRODUCTNAME>TEST NAME</PRODUCTNAME>\n"+
        "      <DESCRIPTION>TEST DESCRIPTION</DESCRIPTION>\n"+
        "      <KEYWORDS>\n"+
        "        <KEYWORD>keyword_1</KEYWORD>\n"+
        "        <KEYWORD>keyword_2</KEYWORD>\n"+
        "        <KEYWORD>keyword_3</KEYWORD>\n"+
        "        <KEYWORD>keyword_n</KEYWORD>\n"+
        "      </KEYWORDS>\n"+  // <--
        "    </PRODUCT>\n"+
        "  </PRODUCTS>\n"+
        "</CATALOG>";
    XStream xstream = new XStream();

    xstream.alias("KEYWORD", String.class);  // <--
    xstream.processAnnotations(Catalog.class);
    xstream.processAnnotations(Product.class);

    Catalog catalog = (Catalog) xstream.fromXML(xml);
    System.out.println(catalog.products.get(0).keywords.get(0));
}