2012-07-30 57 views
0

我在這裏工作的問題,得到的大小計算價格。我只需要考慮屬性價格,但我無法弄清楚如何走得更遠。我試過做一個嵌套for循環,但一直不成功。JAVA DOM xml屬性

<?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> 

我爲我的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); 

      } 
     } 
} 

回答

1

如果你只在比薩節點有興趣,你可以使用的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); 
      } 

     } 
}