Guest User

Untitled

a guest
Oct 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 KB | None | 0 0
  1. import java.io.File;
  2.  
  3. import javax.xml.parsers.DocumentBuilderFactory;
  4.  
  5. import org.w3c.dom.Document;
  6. import org.w3c.dom.Node;
  7.  
  8. public class SeedParser{
  9.  
  10.     // Strings
  11.     static String castleId = "castle";
  12.     static String cropId ="crop";
  13.     static String seedId ="seed_id";
  14.     static String matureId ="mature_id";
  15.     static String reward1 ="reward1";
  16.     static String reward2 ="reward2";
  17.     static String alternative ="alternative";
  18.     static String level ="level";
  19.     static String limitSeed ="limit_seed";
  20.     static String limitCrops ="limit_crops";
  21.  
  22.     public static void main (String argv [])
  23.     {
  24.         parseSeeds();
  25.     }
  26.    
  27.     /**
  28.      *
  29.      *
  30.     <SEEDS>
  31.     <castle id="1">
  32.     <crop id="5073">         <!-- Dark Coda -->
  33.     <seed_id val="5016" />   <!-- Seed: Dark Coda -->
  34.     <mature_id val="5103" /> <!-- Mature Dark Coda -->
  35.     <reward1 val="1864" />   <!-- Stem -->
  36.     <reward2 val="1878" />   <!-- Braided Hemp -->
  37.     <alternative val="0" />
  38.     <level val="10" />
  39.     <limit_seed val="8100" />
  40.     <limit_crops val="9000" />
  41.     </crop>
  42.     </castle>
  43.     </SEEDS
  44.      */
  45.     public static void parseSeeds()
  46.     {
  47.  
  48.         try {
  49.             DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  50.             docBuilderFactory.setValidating(false);
  51.             docBuilderFactory.setIgnoringComments(true);
  52.             docBuilderFactory.setIgnoringElementContentWhitespace(true);
  53.             File file = new File("seeds.xml");
  54.  
  55.             Document doc = null;
  56.  
  57.             doc = docBuilderFactory.newDocumentBuilder().parse(file);
  58.             doc.getDocumentElement().normalize ();
  59.  
  60.             // SEED File
  61.             for(Node a = doc.getFirstChild(); a != null; a = a.getNextSibling())
  62.             {
  63.                 if ("SEEDS".equalsIgnoreCase(a.getNodeName()))
  64.                 {
  65.                     // Castle
  66.                     for(Node b = a.getFirstChild(); b != null; b = b.getNextSibling())
  67.                     {
  68.                         if(castleId.equalsIgnoreCase(b.getNodeName()))
  69.                         {
  70.                             // Crop
  71.                             for(Node c = b.getFirstChild(); c != null; c = c.getNextSibling())
  72.                             {
  73.                                 if (cropId.equalsIgnoreCase(c.getNodeName()))
  74.                                 {
  75.                                     int cropIdVal = Integer.parseInt(c.getAttributes().getNamedItem("id").getNodeValue());
  76.                                     // System.out.println(cropIdVal);
  77.  
  78.                                     int seedIdVal = 0;
  79.                                     int matureIdVal = 0;
  80.                                     int type1RVal = 0;
  81.                                     int type2RVal = 0;
  82.                                     int isAltVal = 0;
  83.                                     int levelVal = 0;
  84.                                     int limitSeedsVal = 0;
  85.                                     int limitCropsVal = 0;
  86.  
  87.                                     // System.out.println("Parser got Crop ID");
  88.  
  89.                                     // Attributes
  90.                                     for(Node d = c.getFirstChild(); d != null; d.getNextSibling())
  91.                                     {
  92.                                         // System.out.println("Parser got in Crops")
  93.                                         if(d.getNodeName().equals("seed_id"))
  94.                                         {
  95.                                             seedIdVal = Integer.parseInt(d.getAttributes().getNamedItem("val").getNodeValue());
  96.                                         }
  97.                                         else if (d.getNodeName().equalsIgnoreCase("mature_id"))
  98.                                         {
  99.                                             matureIdVal = Integer.parseInt(d.getAttributes().getNamedItem("val").getNodeValue());
  100.                                         }
  101.                                         else if (d.getNodeName().equalsIgnoreCase("reward1"))
  102.                                         {
  103.                                             type1RVal = Integer.parseInt(d.getAttributes().getNamedItem("val").getNodeValue());
  104.                                         }
  105.                                         else if (d.getNodeName().equalsIgnoreCase("reward2"))
  106.                                         {
  107.                                             type2RVal = Integer.parseInt(d.getAttributes().getNamedItem("val").getNodeValue());
  108.                                         }
  109.                                         else if (d.getNodeName().equalsIgnoreCase("alternative"))
  110.                                         {
  111.                                             isAltVal = Integer.parseInt(d.getAttributes().getNamedItem("val").getNodeValue());
  112.                                         }
  113.                                         else if (d.getNodeName().equalsIgnoreCase("level"))
  114.                                         {
  115.                                             levelVal = Integer.parseInt(d.getAttributes().getNamedItem("val").getNodeValue());
  116.                                         }
  117.                                         else if (d.getNodeName().equalsIgnoreCase("limit_seed"))
  118.                                         {
  119.                                             limitSeedsVal = Integer.parseInt(d.getAttributes().getNamedItem("val").getNodeValue());
  120.                                         }
  121.                                         else if (d.getNodeName().equalsIgnoreCase("limit_crops"))
  122.                                         {
  123.                                             limitCropsVal = Integer.parseInt(d.getAttributes().getNamedItem("val").getNodeValue());
  124.                                         }
  125.  
  126.  
  127.                                         // System.out.println("Parser got out of Crops");
  128.                                     }
  129.                                     System.out.println("system is out of crop elements");
  130.                                     System.out.println("<crop id =X"+cropIdVal +"X seedId=X"+seedIdVal+"X mature_Id=X"+ matureIdVal + "X reward1=X"+ type1RVal+"X reward2=X"+ type2RVal + "X alternative=X"+isAltVal+"X level=X"+ levelVal+ "X limit_seed=X"+ limitSeedsVal+ "X limit_crops=X"+limitCropsVal+"X />");
  131.                                 }
  132.                             }
  133.                         }
  134.                     }
  135.                 }
  136.             }
  137.         }catch (Throwable t) {
  138.             t.printStackTrace ();
  139.         }
  140.  
  141.     }
  142. }
Add Comment
Please, Sign In to add comment