Advertisement
alexjowilson7

CSCI345

Feb 21st, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. Document doc = null;
  2.             parseCards parsing = new parseCards();
  3.             try {
  4.                 doc = parsing.getDocFromFile("cards.xml");
  5.                 parsing.readCardData(doc);
  6.             } catch (Exception e) {
  7.                 System.out.println("Error = " + e);
  8.             }
  9. ///////////////////////////////////////////////////////
  10. ///////////////////////////////////////////////////////
  11.  
  12.  
  13. import java.util.ArrayList;
  14.  
  15. import javax.xml.parsers.DocumentBuilder;
  16. import javax.xml.parsers.DocumentBuilderFactory;
  17. import javax.xml.parsers.ParserConfigurationException;
  18.  
  19. import org.w3c.dom.Document;
  20. import org.w3c.dom.Element;
  21. import org.w3c.dom.Node;
  22. import org.w3c.dom.NodeList;
  23.  
  24. public class parseCards {
  25.  
  26.     // these 2 methods will parse cards.XML file
  27.  
  28.         public Document getDocFromFile(String filename) throws ParserConfigurationException {
  29.             {
  30.                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  31.                 DocumentBuilder db = dbf.newDocumentBuilder();
  32.                 Document doc = null;
  33.                 try {
  34.                     doc = db.parse(filename);
  35.                 } catch (Exception ex) {
  36.                     System.out.println("XML parse failure");
  37.                     ex.printStackTrace();
  38.                 }
  39.                 return doc;
  40.             } // exception handling
  41.         }
  42.  
  43.         public Card[] readCardData(Document d) {
  44.  
  45.             Card[] crd = new Card[40];
  46.  
  47.             Element root = d.getDocumentElement();
  48.             NodeList cards = root.getElementsByTagName("card");
  49.             // card loop
  50.             for (int i = 0; i < crd.length; i++) {
  51.  
  52.                 Card curCard = new Card();
  53.                 ArrayList<Role> roles = new ArrayList<Role>();
  54.  
  55.                 // read data from nodes
  56.                 // card name
  57.                 Node card = cards.item(i);
  58.  
  59.                 // SET CARD NAME
  60.                 String cardName = card.getAttributes().getNamedItem("name").getNodeValue();
  61.                 curCard.setName(cardName);
  62.                 System.out.println(cardName);
  63.  
  64.                 // SET CARD BUDGET
  65.                 String budget = card.getAttributes().getNamedItem("budget").getNodeValue();
  66.                 // TypeCasting
  67.                 int budgetResult = Integer.parseInt(budget);
  68.                 curCard.setBudget(budgetResult);
  69.  
  70.                 // SET FLIPPED
  71.                 curCard.setFlipped(false);
  72.                 int roleCount = 0;
  73.                
  74.                 // SET SCENE NUMBER
  75.                
  76.                
  77.  
  78.                 // reads data
  79.                 // loops for roles
  80.                 NodeList children = card.getChildNodes();
  81.                 for (int j = 0; j < children.getLength(); j++) {
  82.                     Node sub = children.item(j);
  83.  
  84.                     if ("scene number".equals(sub.getNodeName())) {
  85.                         // String sceneNumber =
  86.                         // sub.getAttributes().getNamedItem("number").getNodeValue();
  87.  
  88.                         // SET SCENE DESCRIPTION
  89.                         String sceneDescription = sub.getTextContent();
  90.                         curCard.setDescription(sceneDescription);
  91.  
  92.                     }
  93.  
  94.                     else if ("part name".contentEquals(sub.getNodeName())) {
  95.                         //
  96.                         roleCount++; // counts how many roles there are on a card
  97.                         // SET PART NAME
  98.                         String partName = sub.getAttributes().getNamedItem("name").getNodeValue();
  99.                         roles.get(j).setRoleDescription(partName);
  100.  
  101.                         // SET RANK REQUIRED
  102.                         String level = sub.getAttributes().getNamedItem("level").getNodeValue();
  103.                         // Typecasting
  104.                         int levelResult = Integer.parseInt(level);
  105.                         // setRank
  106.                         roles.get(j).setRank(levelResult);
  107.  
  108.                         // SET LINE SAID
  109.                         String lineName = sub.getTextContent();
  110.                         roles.get(j).setLine(lineName);
  111.                        
  112.                         roles.get(j).setOnCard(true);
  113.  
  114.                     }
  115.                 }
  116.                 curCard.setNumRoles(roleCount);
  117.                 curCard.setRoles(roles);
  118.                 crd[i] = curCard;
  119.  
  120.             }
  121.             return crd;
  122.         }  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement