无法从XML节点填充List <item>

时间:2015-11-23 08:02:00

标签: java xml domparser

我有一个要解析的xml文档,它有我试过的嵌套节点,但无法按需要完成工作。 XML doc是

<Items>
   <Item>
   <MediumImage>
      <URL>http://ecx.images-amazon.com/images/I/51l7DDD1qNL._SL160_.jpg</URL>
      <Height Units="pixels">160</Height>
      <Width Units="pixels">160</Width>
   </MediumImage>
   <Title>Fallout 4 Vault Dweller's Survival Guide Collector's Edition: Prima Official Game Guide</Title>
   <OfferSummary>
   <LowestNewPrice>
   <Amount>1952</Amount>
   </OfferSummary>
   </Item>
   .
   .
   .
</Items>

我已经完成了将 Title 节点值提取为

private static NodeList fetchTitle(String requestUrl) {
        NodeList nodeList = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(requestUrl);
            nodeList = doc.getElementsByTagName("Title");            
        } catch (Exception e) {
            System.out.println("Message is " + e.getCause() + "...." + e.getMessage());
            throw new RuntimeException(e);
        }
        return nodeList;
    }

我在main()中打印为

titleList = fetchTitle(requestUrl);        
        for (int i = 0; i < titleList.getLength(); i++) {
            Node node = titleList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                // do something with the current element
                System.out.println(node.getNodeName()); 
                System.out.println("Signed Title is \"" + node.getTextContent() + "\"");
                System.out.println();
            }

        }
来自 LowestNewPrice 节点的

Amount 值为

private static NodeList fetchPrice(String requestUrl) {
        NodeList nodeList = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(requestUrl);
            nodeList = doc.getElementsByTagName("LowestNewPrice");           
        } catch (Exception e) {
            System.out.println("Message is " + e.getCause() + "...." + e.getMessage());
            throw new RuntimeException(e);
        }
        return nodeList;
    }

我在main()中打印为

priceList = fetchPrice(requestUrl);

        for (int i = 0; i < priceList.getLength(); i++) {
            Node node = priceList.item(i).getFirstChild();
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                // do something with the current element
                System.out.println(node.getNodeName()); 
                System.out.println("Signed Price is \"" + node.getTextContent() + "\"");
                System.out.println();
            }

        }

通过上面的代码我首先获得所有 Title 值,然后我分别获得 Amount 值,但我真正想要的是将POJO类作为

public class Item {

    String title;
    String price;

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }

}

并使用setTitle(), setPrice()向Item对象添加值,并返回List<Item> 请帮助。

3 个答案:

答案 0 :(得分:2)

可能请尝试此解决方案。 解析您的数据并添加到List<Item> as:

public static Document fetchRequiredData(String src) {
    Document doc = null;

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser;

    try {
        parser = dbFactory.newDocumentBuilder();
        doc= parser.parse(src);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return doc;
}

public static List<Item>  parseItemInfo(Document doc){

    List<Item> items = new ArrayList<Item>();
    NodeList itemNodeList = doc.getElementsByTagName("Item");       

    for (int i = 0; i < itemNodeList.getLength(); i++) {

        Node titleNode = doc.getElementsByTagName("Title").item(i);
        Node priceNode = doc.getElementsByTagName("LowestNewPrice").item(i).getFirstChild();

        if (titleNode.getNodeType() == Node.ELEMENT_NODE || priceNode.getNodeType() == Node.ELEMENT_NODE ) {
            Item item = new Item();
            item.setDesc(titleNode.getTextContent());
            item.setPrice(priceNode.getTextContent());
            items.add(item);
        }
    }
    return items;
}

现在您的列表已准备好在main()方法中进行测试

public static void main(String[] args) {
     List<Item> items = parseItemInfo(fetchRequiredData(requestUrl));
     System.out.println("Printing List<Item> contents ...");
     for (Item item : items) {
         System.out.println("Title is " + item.getTitle()); 
         System.out.println("Price is " + item.getPrice());
         System.out.println();
     }
}

希望这个有所帮助。

答案 1 :(得分:2)

您可以使用JAXBContext实现这一目标。首先创建Item类。

@XmlRootElement(name = "Items")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class Items
{
    @XmlElement(name = "Item")
    private List<Item> item;

    public void setItem(List<Item> itemList)
    {
        this.item = itemList;
    }
    public List<Item> getItem()
    {
        return this.item;
    }
}

@XmlRootElement(name = "Item")
@XmlAccessorType(vallue = XmlAccessType.FIELD)
public class Item
{
    @XmlElement(name = "MediumImage")
    private MediumImage image;
    @XmlElement(name = "Title")
    private String title;
    @XmlElement(name = "OfferSummary")
    private OfferSummary summary;

    getters();
    setters();
}

@XmlRootElement(name = "MediumImage")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class MediumImage
{
    @XmlElement(name = "URL")
    private String url;
    ....
}

@XmlRootElement(name = "OfferSummary")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class OfferSummary
{
    @XmlElement(name = "LowestNewPrice")
    private LowestNewPrice lowestPrice;
    ....
}

然后从main方法使用marshaller和unmarshaller方法。

public static void main(String[] args)
{
    File xmlFile = new File("file path");
    JAXBContext context = JAXBContext.newInstance(Items.class);
    //To get POJO from xml
    Unmarshaller unmarshaller = context.createUnmarshaller();
    Items items = (Items) unmarshaller.unmarshal(xmlFile);
}

答案 2 :(得分:1)

目前您似乎将价格和标题分成2个列表,如果您想将商品的价格和标题存储到单个Item对象中,您可以执行类似的操作这样:

public class Item {

    public static void main(String[] args) {
        ArrayList<Item> items = new ArrayList<Item>();
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new File("items.xml"));
            NodeList itemElements = doc.getElementsByTagName("Item");

            for (int i = 0; i < itemElements.getLength(); i++) {
                Node itemElement = itemElements.item(i);
                NodeList itemChildren = itemElement.getChildNodes();

                Item item = new Item();

                for (int j = 0; j < itemChildren.getLength(); j++) {
                    Node n = itemChildren.item(j);
                    if (n.getNodeName().equalsIgnoreCase("title")) {
                        item.setTitle(n.getTextContent());
                    } else if (n.getNodeName().equalsIgnoreCase("OfferSummary")) {
                        NodeList offerChildren = n.getChildNodes();
                        for (int k = 0; k < offerChildren.getLength(); k++) {
                            Node offerChild = offerChildren.item(k);
                            if (offerChild.getNodeName().equalsIgnoreCase("LowestNewPrice")) {
                                item.setPrice(offerChild.getTextContent());
                            }
                        }
                    }
                }
                items.add(item);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("items: " + items);
    }

    String title;
    String price;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Title: " + title + " Price: " + price;
    }

}

这样做是为了从XML中获取所有<Item>元素并循环遍历它们以获取项目的标题和价格。