我的代码的对象是根据存储在xml文件中的食谱创建购物清单。
我在如何从xml文件中获取数据并将其转换为列表时遇到问题。
首先,我会喜欢一些帮助,只需在第一个食谱中打印一份简单的成分列表。
继承我的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<Drinks>
<Drink id="Whiskey Sour">
<ingredients group="Alkohol">
<ingredient name="Whiskey" quantity="5" unit="cl">Whiskey</ingredient>
</ingredients>
<ingredients group="Mixer">
<ingredient name="Friskpresset Citron" quantity="4" unit="cl">Friskpresset Citron</ingredient>
<ingredient name="Sukkersirup" quantity="3" unit="cl">Sukkersirup</ingredient>
<ingredient name="Æggehvide" quantity="½" unit="cl">Æggehvide</ingredient>
<ingredient name="Angustura" quantity="2" unit="dash">Angustura</ingredient>
</ingredients>
</Drink>
<Drink id="White Russian Frangelico">
<ingredients group="Alkohol">
<ingredient name="Vodka" quantity="3" unit="cl">Vodka</ingredient>
<ingredient name="Kahlua" quantity="2" unit="cl">Kahlua</ingredient>
<ingredient name="Frangelico" quantity="1" unit="cl">Frangelico</ingredient>
</ingredients>
<ingredients group="Mixer">
<ingredient name="Sødmælk" quantity="3" unit="cl">Sødmælk</ingredient>
</ingredients>
</Drink>
<Drink id="Espresso Martini">
<ingredients group="Alkohol">
<ingredient name="Vodka" quantity="3" unit="cl">Vodka</ingredient>
<ingredient name="Kahlua" quantity="2" unit="cl">Kahlua</ingredient>
</ingredients>
<ingredients group="Mixer">
<ingredient name="Kaffe" quantity="2" unit="cl">Kaffe</ingredient>
<ingredient name="Sukkersirup" quantity="1" unit="cl">Sukkersirup</ingredient>
</ingredients>
</Drink>
</Drinks>
这是我的代码:
import java.io.File;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class CreateListOfGoods{
public static void main (String argv[]) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("src/Kartotek.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
//Prints the amount of drinks in the database
NodeList listOfDrinksNode = doc.getElementsByTagName("Drink");
int totalDrinks = listOfDrinksNode.getLength();
System.out.println("Total no of Drinks : " + totalDrinks);
for(int s=0; s<listOfDrinksNode.getLength(); s++) {
Node listOfIngredientsNode = listOfDrinksNode.item(s);
if(listOfIngredientsNode.getNodeType() == Node.ELEMENT_NODE) {
System.out.println("Virker if?");
//--------
NodeList alkoholList = doc.getElementsByTagName("Ingredient");
Element alkoholElement = (Element)alkoholList.item(0);
String PrintAlkohol = alkoholElement.getTagName();
System.out.println(PrintAlkohol);
}
}
} catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
} catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
} catch (Throwable t) {
t.printStackTrace ();
}
}
}
有些变量是丹麦语,但代码应该是可以理解的。希望你们中的一些人可以指导我的方向
答案 0 :(得分:0)
我稍微修改了代码。现在,它将所有成分收集到列表中,该列表在程序结束时打印出来。它分三步完成: 1)找到所有饮料; 2)找到所有成分组; 3)找到这些组中的所有成分并将它们添加到列表中。
import org.w3c.dom.*;
import java.util.List;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class CreateListOfGoods{
public static void main (String argv[]) {
try {
List<String> listOfIngredients = new ArrayList<String>();
String INGREDIENTS_TAG_NAME = "ingredients";
String INGREDIENT_TAG_NAME = "ingredient";
String DRINK_TAG_NAME = "Drink";
String GROUP_ATTRIBUTE_NAME = "group";
String INGREDIENT_NAME_ATTRIBUTE_NAME = "name";
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new java.io.File("src/Kartotek.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
//Prints the amount of drinks in the database
NodeList listOfDrinksNode = doc.getElementsByTagName(DRINK_TAG_NAME);
int totalDrinks = listOfDrinksNode.getLength();
System.out.println("Total no of Drinks : " + totalDrinks);
for(int s=0; s<listOfDrinksNode.getLength(); s++) {
// Find all ingredients groups
NodeList groupNodes = listOfDrinksNode.item(s).getChildNodes();
for(int i = 0; i < groupNodes.getLength(); i++) {
if(groupNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element groupElement = (Element)groupNodes.item(i);
if(groupElement.getTagName().equals(INGREDIENTS_TAG_NAME)) {
System.out.println(String.format("group: %s", groupElement.getAttribute(GROUP_ATTRIBUTE_NAME)));
// Find all ingredients in group
NodeList ingredientNodes = groupElement.getChildNodes();
for(int j = 0; j < ingredientNodes.getLength(); j++) {
if(ingredientNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
Element ingredientElement = (Element)ingredientNodes.item(j);
if(ingredientElement.getTagName().equals(INGREDIENT_TAG_NAME)) {
String ingredientName = ingredientElement.getAttribute(INGREDIENT_NAME_ATTRIBUTE_NAME);
System.out.println(String.format("Ingredient name: %s", ingredientName));
listOfIngredients.add(ingredientName);
}
}
}
}
}
}
}
System.out.println("All ingredients: " + listOfIngredients.toString());
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
}
}