Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:获取嵌套xml文件中的子节点值之和_Java_Xml_Dom - Fatal编程技术网

Java:获取嵌套xml文件中的子节点值之和

Java:获取嵌套xml文件中的子节点值之和,java,xml,dom,Java,Xml,Dom,我需要制作一个程序,输出xml文件中特定元素的价格。 xml文件如下所示: <list name="root"> <book name="B1" price="30" isbn="123"/> <list name="L1"> <book name="B2" price="20" isbn="234"/> <list name="L2"> <cd name="C1" price="15"/> <cd nam

我需要制作一个程序,输出xml文件中特定元素的价格。 xml文件如下所示:

<list name="root">
<book name="B1" price="30" isbn="123"/>
<list name="L1">
 <book name="B2" price="20" isbn="234"/>
 <list name="L2">
  <cd name="C1" price="15"/>
  <cd name="C2" price="5"/>
  <book name="B3" price="10" isbn="345"/>
 </list>
 <cd name="C3" price="15"/>
 <book name="B4" price="60" isbn="456"/> 
</list>
</list>
如何获取嵌套列表的值?
谢谢大家!

由于
list
包含子属性,因此从
nList.getLength()-1
循环到
0
将避免许多问题

对于列表,我们需要子属性
book
cd
的值(价格)。因此,从最后一个循环到第一个循环将有助于我们将子属性的值存储在
数据中
作为优先步骤

现在,为了得到列表的总价格,我们迭代了所有书籍和cd的节点列表。 我们将构成清单价格的所有值相加

下面是
(e.getTagName()=“list”&&n.hasChildNodes()
)的代码

NodeList books=e.getElementsByTagName(“book”);
NodeList cd=e.getElementsByTagName(“cd”);
System.out.println(books.getLength());
System.out.println(cd.getLength());
双倍标价=0;

对于(int i=0;我非常感谢你!我认为这将解决我的问题。很高兴它有帮助!!:)
public class ManageList implements Assignment7 {

    private HashMap<String, Double> data = new HashMap<String, Double>();


    @Override
    public void loadXml(File input) throws Exception {

        // given in the readme
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        // get filename => absolute path
        String filename = input.getAbsolutePath();

        Document doc = db.parse(new File(filename));

        // Normalize the XML Structure
        doc.getDocumentElement().normalize();

        // get the root element from XML document
        // Element root = doc.getDocumentElement();

        // ####################################
        // acces elements and their attributes and store it in a hashmap
        // ####################################

        NodeList nl = doc.getElementsByTagName("*");

        storeNodes(nl);

        //System.out.println(Arrays.asList(data)); 

    }

    @Override
    public Optional<Double> getPrice(String item) {

        return null;
    }

    public void storeNodes(NodeList nl) {

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);
            int type = n.getNodeType();
            if (type == Node.ELEMENT_NODE) {
                Element e = (Element) n;

                if (e.getTagName() == "book" || e.getTagName() == "cd") {

                    data.put(e.getAttribute("name"), Double.parseDouble(e.getAttribute("price")));
                }

                if (e.getTagName() == "list" && n.hasChildNodes()) {

                    String name = e.getAttribute("name");

                    //here i get a NumberFormatException
                    //data.put(name, Double.parseDouble(e.getAttribute("price")));


                    //just to show output
                    data.put(name, 0.0);



                }

                storeNodes(n.getChildNodes());
            }

        }

    }
[{B2=20.0, C3=15.0, B3=10.0, B4=60.0, L1=0.0, L2=0.0, root=0.0, C1=15.0, B1=30.0, C2=5.0}]
NodeList books = e.getElementsByTagName("book");
NodeList cd = e.getElementsByTagName("cd");
System.out.println(books.getLength());
System.out.println(cd.getLength());

double listPrice = 0;
for(int i=0;i<books.getLength();i++) {
 Node t = books.item(i);
 Element e1 = (Element)t;

 /**This can be reduced if we loop from nList.getLength()-1 to 0, Since the data already exists in data.
 //if (!data.containsKey(e1.getAttribute("name"))){
 //    data.put(e1.getAttribute("name"),Double.parseDouble(e1.getAttribute("price")));
 //
 //}
 */


 listPrice += Double.parseDouble(e1.getAttribute("price"));
}

for(int i=0;i<cd.getLength();i++){
 Node t = cd.item(i);
 Element e1 = (Element)t;
 listPrice += Double.parseDouble(e1.getAttribute("price"));
}