sja91

Untitled

Mar 9th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6.  
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.Node;
  10. import org.w3c.dom.NodeList;
  11.  
  12.  
  13.  
  14.  
  15. public class main {
  16.     public static void main(String args[]) throws Exception
  17.     {
  18.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  19.         DocumentBuilder builder = factory.newDocumentBuilder();
  20.         Document document =
  21.                       builder.parse(
  22.                         ClassLoader.getSystemResourceAsStream("boots.xml"));
  23.        
  24.         //List<Boot> bootList = new ArrayList<>();
  25.         NodeList nodeList = document.getDocumentElement().getChildNodes();
  26.        
  27.        
  28.        
  29.         for (int i = 0; i < nodeList.getLength(); i++)
  30.         {
  31.             Node node = nodeList.item(i);
  32.             if (node.getNodeType() == Node.ELEMENT_NODE)
  33.             {
  34.                 Element elem = (Element)node;
  35.                 Element stat = (Element)elem.getChildNodes().item(1);
  36.            
  37.                 String name = elem.getAttribute("name");
  38.                 int    code = Integer.parseInt( elem.getAttribute("code"), 2);
  39.                
  40.                 String atr = stat.getAttribute("name");
  41.                 double     val = Double.parseDouble(( stat.getTextContent()));
  42.                
  43.                 //bootList.add(new Boot(name, code, atr, val));
  44.                 System.out.println(new Boot(name, code, atr, val));
  45.             }
  46.         }
  47.     }
  48. }
  49.  
  50. class Boot
  51. {
  52.     private String Name, Atr;
  53.     private int Code;
  54.     private double Val;
  55.    
  56.     public Boot( String name, int code , String atr, double val)
  57.     {
  58.         Name = name;
  59.         Code = code;
  60.         Atr = atr;
  61.         Val = val;
  62.     }
  63.    
  64.     public String toString()
  65.     {
  66.         return "Boots: " + Name + " Code: " + Code + " Stat: " + Atr + " " + Val;  
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment