Kimossab

XML Parsing System

Mar 12th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.75 KB | None | 0 0
  1. //ReadXML (MainActivity.java or whatever you want...)
  2.     public void ReadXML(View V)
  3.     {
  4.         XMLParsing x = new XMLParsing();
  5.         File DirName = new File(Environment.getExternalStorageDirectory(),"LaData");
  6.         File fich = new File(DirName,"LaData.xml");
  7.         try
  8.         {
  9.             ArrayList<XMLObject> data = x.parse(new BufferedInputStream(new FileInputStream(fich)));
  10.             if(data==null)
  11.                 return;
  12.             for(int i=0; i<data.size(); i++)
  13.             {
  14.                 ArrayList<CPValor> CP = data.get(i).GetCampos();//ERROR
  15.                 for(int n = 0; n < CP.size(); n++)
  16.                     Log.i(""+data.get(i).GetNome(), ""+CP.get(n).GetCampo() + CP.get(n).GetValor());
  17.             }
  18.         }
  19.         catch(Exception e)
  20.         {
  21.             Log.e("Error reading XML:",""+e.getMessage()); //don't worry, "e" won't show you any message whatsoever if it fails... stupid little fuck that guy... I'll try to make him tell something in another time...
  22.         }
  23.     }
  24.  
  25. //Classe CPValor (CPValor.java)
  26. public class CPValor
  27. {
  28.     private String Campo;
  29.     private String Valor;
  30.  
  31.     //C = Campo; V = Valor
  32.     public CPValor(String C, String V)
  33.     {
  34.         Campo = C;
  35.         Valor = V;
  36.     }
  37.  
  38.     public CPValor(String C)
  39.     {
  40.         Campo = C;
  41.         Valor = "";
  42.     }
  43.     public CPValor()
  44.     {
  45.         Campo = "";
  46.         Valor = "";
  47.     }
  48.  
  49.     public String GetCampo() {return Campo;}
  50.     public String GetValor() {return Valor;}
  51.     public void SetCampo(String C) {Campo = C;}
  52.     public void SetValor(String V) {Valor = V;}
  53. }
  54.  
  55. //Classe XMLObject (XMLObject.java)
  56. public class XMLObject
  57. {
  58.     String nome;
  59.     ArrayList<CPValor> LCamp = null;
  60.  
  61.     public XMLObject(String nom)
  62.     {
  63.         nome = nom;
  64.         LCamp = new ArrayList<CPValor>();
  65.     }
  66.     public XMLObject()
  67.     {
  68.         nome = "";
  69.         LCamp = new ArrayList<CPValor>();
  70.     }
  71.  
  72.     void Add(CPValor P)
  73.     {
  74.         LCamp.add(P);
  75.     }
  76.  
  77.     ArrayList<CPValor> GetCampos()
  78.     {
  79.         return LCamp;
  80.     }
  81.  
  82.     String GetNome()
  83.     {
  84.         return nome;
  85.     }
  86.  
  87.     String GetVal(String c)
  88.     {
  89.         for(int i=0; i<LCamp.size(); i++)
  90.         {
  91.             if(LCamp.get(i).GetCampo() == c)
  92.                 return LCamp.get(i).GetValor();
  93.         }
  94.         return "";
  95.     }
  96. }
  97.  
  98. //XMLParsing.java
  99. public class XMLParsing
  100. {
  101.     private ArrayList<XMLObject> Obj = new ArrayList<>(); //The array of object we found on the xml
  102.  
  103.     public ArrayList<XMLObject> parse(InputStream in) throws XmlPullParserException, IOException
  104.     {
  105.         try
  106.         {
  107.             XmlPullParser parser = Xml.newPullParser(); //Pull parser to read the file
  108.             parser.setInput(in, null); //we need to set the input to the file
  109.             parser.nextTag(); //skip the first tag (that thing about the coding and stuff I believe)
  110.             readFeed(parser); //Let's read the thing
  111.             return Obj; //Now we can freely return the obj
  112.         }
  113.         finally
  114.         {
  115.             in.close(); //We always gotta close the file
  116.         }
  117.     }
  118.  
  119.     private void readFeed(XmlPullParser parser) throws XmlPullParserException, IOException
  120.     {
  121.         int num = 0; //the branch we are 1st, 2nd or 3rd...
  122.         /*
  123.             num 1 : Doc Start
  124.             num 2 : Object start
  125.             num 3 : Object info
  126.          */
  127.         int eventType = parser.getEventType(); //self explanatory afterwards
  128.         //prevent "possibly not initialized" errors (facepalm) too protective...
  129.     //MAIN UPDATE V the stupid fuck doesn't like the null... for some reason even if the file is empty you gotta initialize these things, (if the file doesn't exist you won't even need them) make sure the file has something to be read for not wasting this stuff, but whatever fuck it...
  130.         CPValor n = new CPValor();
  131.         XMLObject ob = new XMLObject();
  132.         /*
  133.                 Adapt the following code according to your needs.
  134.                 Currently it only reads xml according to the following format:
  135.                 <Start>
  136.                         <Object>
  137.                                 <Info>TEXT</info>
  138.                         </Object>
  139.                 </Start>
  140.  
  141.                 if you got a info inside a info (a 4th branch as I call them) then that one will be read but not stored
  142.                 like this:
  143.                 <Start>
  144.                         <Object>
  145.                                 <Info>
  146.                                         <Info1>TEXT1</Info1>
  147.                                         <Info2>TEXT2</Info2>
  148.                                 <Info>
  149.                                 <SInfo>TEXT3</SInfo>
  150.                         </Object>
  151.                 </Start>
  152.  
  153.                 Object will be read and stored as a XMLObject name
  154.                 Info and SInfo will be read and stored as 2 CPValor inside the last XMLObjec (May crash if you don't addapt your code, didn't test, won't test)
  155.                 Info1 and Info2 WILL be read BUT NOT stored anywhere...
  156.                 You can do it by different ways, create another class to stor those values (copy paste from CPValor if needed) or just stor the 3rd and 4th branches instead of the 2nd and 3rd or just thik in anyother way, you choose my friend
  157.         */
  158.         while (eventType != XmlPullParser.END_DOCUMENT)
  159.         {
  160.             if(eventType == XmlPullParser.START_TAG) //start of a tag (<Start> for example)
  161.             {
  162.                 num++; //start a tag then it's a new branch
  163.                 if(num == 2) //read above that huge comment
  164.                     ob.SetNome(parser.getName());
  165.                 if(num == 3)
  166.                     n.SetCampo(parser.getName());
  167.             }
  168.             else if(eventType == XmlPullParser.END_TAG) //end of a tag (</Start> for example)
  169.             {
  170.         //Side Note. The following news should NOT be here but in the START_TAG BUT it gave me a few logical (but without logic -.-) errors and resulted in data loss. Therefor FOR NOW we'll use the news there, I'll try to update the code ASAP
  171.                 if(num == 3) //bah...
  172.                 {
  173.                     ob.Add(n); //add the CPValor to the XMLObject
  174.                     n = new CPValor(); // create a new CPValor
  175.                 }
  176.                 if(num == 2)
  177.                 {
  178.                     Obj.add(ob); //add the XMLObject to the array
  179.                     ob = new XMLObject(); //Create a new XML
  180.                 }
  181.                 num--; //dah reduce a branch now
  182.             }
  183.             else if(eventType == XmlPullParser.TEXT) //If it's text then it will be the value for CPValue so put it there
  184.                 n.SetValor(parser.getText());
  185.             eventType = parser.next(); //next thing
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment