This is my current XML file it gives me the dialogue for different characters, or at least it should. I want it to work so that I can specify the entity id and the option/quest id and get the output. So what should I do? Any help is appreciated, thank you very much.
<?xml version="1.0"?>
<dialoge>
<entity id="1"> <!-- questgiver -->
<quest id="1">
<option id="1">
<precondition>player has not started quest</precondition>
<output>hello there, can you kill 2 enemies for me?</output>
</option>
<option id="2">
<precondition>player has completed quest and player has not...</precondition>
<output>thankyou, have a sword for your troubles.</output>
</option>
<option id="3">
<precondition>player has not finished quest</precondition>
<output>you haven't finished yet.</output>
</option>
<option id="4">
<outpur>thank you.</outpur>
</option>
</quest>
</entity>
<entity id="2"> <!-- villager -->
<option id="1">
<precondition>village is being destroyed</precondition>
<output>our village is being destroyed, please help us!</output>
</option>
<option id="2">
<precondition>village has been saved or destroyed</precondition>
<output>we will never forget this.</output>
</option>
<option id="3">
<output>hello.</output>
</option>
</entity>
</dialoge>
This is what I currently have, but it does not work. I know this is probably a stupid question, but I couldn't find an answer anywhere on the web. Thanks.
public static void read() {
try {
File file = new File("text.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("root of xml file " + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("entity");
System.out.println("==========================");
for(int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if(element.getElementsByTagName("entity").item(0).getTextContent().equals("output")) {
}
System.out.println("" + getValue("output", element));
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}