Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Document doc = null;
- parseCards parsing = new parseCards();
- try {
- doc = parsing.getDocFromFile("cards.xml");
- parsing.readCardData(doc);
- } catch (Exception e) {
- System.out.println("Error = " + e);
- }
- ///////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////
- import java.util.ArrayList;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- public class parseCards {
- // these 2 methods will parse cards.XML file
- public Document getDocFromFile(String filename) throws ParserConfigurationException {
- {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = null;
- try {
- doc = db.parse(filename);
- } catch (Exception ex) {
- System.out.println("XML parse failure");
- ex.printStackTrace();
- }
- return doc;
- } // exception handling
- }
- public Card[] readCardData(Document d) {
- Card[] crd = new Card[40];
- Element root = d.getDocumentElement();
- NodeList cards = root.getElementsByTagName("card");
- // card loop
- for (int i = 0; i < crd.length; i++) {
- Card curCard = new Card();
- ArrayList<Role> roles = new ArrayList<Role>();
- // read data from nodes
- // card name
- Node card = cards.item(i);
- // SET CARD NAME
- String cardName = card.getAttributes().getNamedItem("name").getNodeValue();
- curCard.setName(cardName);
- System.out.println(cardName);
- // SET CARD BUDGET
- String budget = card.getAttributes().getNamedItem("budget").getNodeValue();
- // TypeCasting
- int budgetResult = Integer.parseInt(budget);
- curCard.setBudget(budgetResult);
- // SET FLIPPED
- curCard.setFlipped(false);
- int roleCount = 0;
- // SET SCENE NUMBER
- // reads data
- // loops for roles
- NodeList children = card.getChildNodes();
- for (int j = 0; j < children.getLength(); j++) {
- Node sub = children.item(j);
- if ("scene number".equals(sub.getNodeName())) {
- // String sceneNumber =
- // sub.getAttributes().getNamedItem("number").getNodeValue();
- // SET SCENE DESCRIPTION
- String sceneDescription = sub.getTextContent();
- curCard.setDescription(sceneDescription);
- }
- else if ("part name".contentEquals(sub.getNodeName())) {
- //
- roleCount++; // counts how many roles there are on a card
- // SET PART NAME
- String partName = sub.getAttributes().getNamedItem("name").getNodeValue();
- roles.get(j).setRoleDescription(partName);
- // SET RANK REQUIRED
- String level = sub.getAttributes().getNamedItem("level").getNodeValue();
- // Typecasting
- int levelResult = Integer.parseInt(level);
- // setRank
- roles.get(j).setRank(levelResult);
- // SET LINE SAID
- String lineName = sub.getTextContent();
- roles.get(j).setLine(lineName);
- roles.get(j).setOnCard(true);
- }
- }
- curCard.setNumRoles(roleCount);
- curCard.setRoles(roles);
- crd[i] = curCard;
- }
- return crd;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement