profiler84
Grünschnabel
Hallo,
Ich versuche ein grafisches Inventar für ein Spiel zu bauen.
Das Inventar wird mit Daten aus einer XML gespeist die folgende Struktur hat:
Diese Daten Lese ich aus und speichere sie in einer ArrayList mit folgendem Code:
In weiterer Folge möchte ich die Daten dann über ein Jframe darstellen. Meine konkrete Frage ist wie kann ich die einzelnen Items und ihre Attribute ansprechen, vorallem
was mache ich wenn Item-Typen öfter vorkommen also im XML <Item><Type>Helmet</Type> öfter vorkommt. Wie verweise ich darauf dass ich z.b. das 2. Item haben will.
Ich komme hier überhaupt nicht weiter.
Über einen Vorschlag zur Umsetzung würde ich mich riesig freuen.
Danke.
Ich versuche ein grafisches Inventar für ein Spiel zu bauen.
Das Inventar wird mit Daten aus einer XML gespeist die folgende Struktur hat:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Inventary-List>
<Item>
<Type>Shield</Type>
<Name>Schild der Rache</Name>
<Description>Das Schild entstammt aus Gondor</Description>
<Weight>5</Weight>
<Attack>1</Attack>
<Attackreq>0</Attackreq>
<Defense>3</Defense>
<Defensereq>2</Defensereq>
<Heal>0</Heal>
<Image>src/Test_packages/img/shield-slot.png</Image>
</Item>
<Item>
<Type>Helmet</Type>
<Name>Helm der Rache</Name>
<Description>Der Helm entstammt aus Gondor</Description>
<Weight>4</Weight>
<Attack>0</Attack>
<Attackreq>0</Attackreq>
<Defense>2</Defense>
<Defensereq>1</Defensereq>
<Heal>0</Heal>
<Image>src/Test_packages/img/helmet-slot.png</Image>
</Item>
<Item>
<Type>Armor</Type>
<Name>Rüstung der Furcht</Name>
<Description>Dieser Armor entstammt aus blub</Description>
<Weight>8</Weight>
<Attack>0</Attack>
<Attackreq>0</Attackreq>
<Defense>4</Defense>
<Defensereq>3</Defensereq>
<Heal>0</Heal>
<Image>src/Test_packages/img/armor-slot.png</Image>
</Item>
</Inventary-List>
Diese Daten Lese ich aus und speichere sie in einer ArrayList mit folgendem Code:
Code:
package Test_packages;
import java.io.*;
import java.util.ArrayList;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
public class GetXMLData{//Start class
//No generics
ArrayList<Item> myItemsList;
Document document;
public GetXMLData (String fName) {//Start constructor
//create a list to hold the Item objects
myItemsList = new ArrayList<Item>();
String fileName = fName;
readXmlFile(fileName);
}//End constructor
private ArrayList readXmlFile (String fName) {
try{// Start try catch
String xmlFile = fName;
File file = new File(xmlFile);
if (file.exists()){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlFile);
Element docRoot = document.getDocumentElement();
//get a nodelist of <employee> elements
NodeList nl = docRoot.getElementsByTagName("Item");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength();i++) {
//get the employee element
Element el = (Element)nl.item(i);
//get the Employee object
Item e = getItem(el);
//add it to list
myItemsList.add(e);
}
}//End if
}
else{
System.out.println("File not found!");
}
}//End try
catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}//End try catch
return myItemsList; }//End void readXmlFile
private Item getItem(Element itm) {
String type = getTextValuebyTag(itm,"Type");
String name = getTextValuebyTag(itm,"Name");
String description = getTextValuebyTag(itm,"Description");
int weight = getIntValuebyTag(itm,"Weight");
int damage = getIntValuebyTag(itm,"Attack");
int attreq = getIntValuebyTag(itm,"Attackreq");
int defense = getIntValuebyTag(itm,"Defense");
int defreq = getIntValuebyTag(itm,"Defensereq");
int heal = getIntValuebyTag(itm,"Heal");
//String type = itm.get
//Create a new Employee with the value read from the xml nodes
Item i = new Item(type,name,description,weight,damage,attreq,defense,defreq,heal);
return i;
}
private String getTextValuebyTag(Element ele, String tagName) {
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if(nl != null && nl.getLength() > 0) {
Element el = (Element)nl.item(0);
textVal = el.getFirstChild().getNodeValue();
}
return textVal;
}
private int getIntValuebyTag(Element ele, String tagName) {
//in production application you would catch the exception
return Integer.parseInt(getTextValuebyTag(ele,tagName));
}
}//End class
was mache ich wenn Item-Typen öfter vorkommen also im XML <Item><Type>Helmet</Type> öfter vorkommt. Wie verweise ich darauf dass ich z.b. das 2. Item haben will.
Ich komme hier überhaupt nicht weiter.
Über einen Vorschlag zur Umsetzung würde ich mich riesig freuen.
Danke.