简体   繁体   English

JAVA DOM xml属性

[英]JAVA DOM xml attributes

I'm working a problem here that gets the size calculates the price. 我正在解决一个问题,该问题由大小计算价格。 I just need to tack on attribute prices, but I'm unable to figure out how to go further. 我只需要考虑属性价格,但是我不知道该怎么做。 I've tried making a nested for loop, but have been unsuccessful. 我曾尝试制作一个嵌套的for循环,但未成功。

<?xml version= "1.0" encoding= "UTF-8" standalone="yes" ?>

<PizzaParlor>
    <Order>
        <Pizza Size="large">
            <Vegetarian>
                <Mushroom Price="1.99">
                </Mushroom>
            </Vegetarian>

        </Pizza>
        <Pizza Size="small">
            <Vegetarian>
                <Spinach Price="1.45">
                </Spinach>
            </Vegetarian>

        </Pizza>
        <Pizza Size="medium">
            <Vegetarian>
                <Pineapple Price="1.79">
                </Pineapple>
            </Vegetarian>

        </Pizza>
        <Pizza Size="small">
            <Vegetarian>
                <Mushroom Price="1.99">
                </Mushroom>
            </Vegetarian>

        </Pizza>
        <Pizza Size="large">
            <Vegetarian>
                <Mushroom Price="1.99">
                </Mushroom>
            </Vegetarian>
        </Pizza>
            <Pizza Size="large">
            <non-vegetarian>
                <Anchovies Price="1.99">
                </Anchovies>
            </non-vegetarian>

        </Pizza>
        <Pizza Size="small">
            <non-vegetarian>
                <Ham Price="1.45">
                </Ham>
            </non-vegetarian>

        </Pizza>
        <Pizza Size="medium">
            <non-vegetarian>
                <Anchovies Price="1.79">
                </Anchovies>
            </non-vegetarian>

        </Pizza>
        <Pizza Size="small">
            <non-vegetarian>
                <Pepperoni Price="2.49">
                </Pepperoni>
            </non-vegetarian>

        </Pizza>
        <Pizza Size="large">
            <non-vegetarian >
                <Chicken Price="2.99">
                </Chicken>
            </non-vegetarian >
        </Pizza>
    </Order>
</PizzaParlor>

I have this for my java program: 我的Java程序有这个:

package pizza;

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class Pizza
{
    public static void main (String [] args)
        throws
        IOException,
        ParserConfigurationException,
        SAXException
        {
            File CFile = new File ("pizzaparlor.xml");
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance ();
            factory.setIgnoringComments (true);
            factory.setIgnoringElementContentWhitespace (true);
            factory.setValidating (true);
            DocumentBuilder builder = factory.newDocumentBuilder ();
            Document document = builder.parse (CFile);
            Element root = document.getDocumentElement ();

            Node orderNode = root.getFirstChild();
            Node Pizza = orderNode.getFirstChild();



            int countofPizzas = 0;

            for(int i = 0 ; Pizza!=null; i ++){

                countofPizzas ++;
                Pizza = Pizza.getNextSibling();

            }
            System.out.println(countofPizzas); 

            double price = 0.0;

            NodeList Orders = root.getFirstChild().getChildNodes();


            for (int i = 0; i < Orders.getLength() ; i++)
            {
                Element pizzaSize = (Element) Orders.item(i);
                String pSize = pizzaSize.getAttribute("Size");


                if (pSize.equalsIgnoreCase("Large"))
                    price = 10.0;
                if (pSize.equalsIgnoreCase("Medium"))
                    price = 7.0;
                if (pSize.equalsIgnoreCase("Small"))
                    price = 5.0;

                System.out.println("Pizza " + i + " " + pSize + " " + price);

            }
        }
}

If you are only interested in the Pizza nodes, you can use the getElementsByTagName() method to return them. 如果仅对Pizza节点感兴趣,则可以使用getElementsByTagName()方法返回它们。

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class Pizza
{
    public static void main (String [] args)
        throws
        IOException,
        ParserConfigurationException,
        SAXException
        {
            File CFile = new File ("pizzaparlor.xml");
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance ();
            factory.setIgnoringComments (true);
            factory.setIgnoringElementContentWhitespace (true);
            factory.setValidating (false);
            DocumentBuilder builder = factory.newDocumentBuilder ();
            Document document = builder.parse (CFile);

            NodeList pizzas = document.getElementsByTagName("Pizza");

            for (int i = 0; i < pizzas.getLength() ; i++)
            {
                Element pizzaSize = (Element) pizzas.item(i);
                String pSize = pizzaSize.getAttribute("Size");

                if (pSize.equalsIgnoreCase("Large"))
                    price = 10.0;
                if (pSize.equalsIgnoreCase("Medium"))
                    price = 7.0;
                if (pSize.equalsIgnoreCase("Small"))
                    price = 5.0;

                System.out.println("Pizza " + i + " " + pSize + " " + price);
            }

        }
}

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

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