Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ReadXML (MainActivity.java or whatever you want...)
- public void ReadXML(View V)
- {
- XMLParsing x = new XMLParsing();
- File DirName = new File(Environment.getExternalStorageDirectory(),"LaData");
- File fich = new File(DirName,"LaData.xml");
- try
- {
- ArrayList<XMLObject> data = x.parse(new BufferedInputStream(new FileInputStream(fich)));
- if(data==null)
- return;
- for(int i=0; i<data.size(); i++)
- {
- ArrayList<CPValor> CP = data.get(i).GetCampos();//ERROR
- for(int n = 0; n < CP.size(); n++)
- Log.i(""+data.get(i).GetNome(), ""+CP.get(n).GetCampo() + CP.get(n).GetValor());
- }
- }
- catch(Exception e)
- {
- 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...
- }
- }
- //Classe CPValor (CPValor.java)
- public class CPValor
- {
- private String Campo;
- private String Valor;
- //C = Campo; V = Valor
- public CPValor(String C, String V)
- {
- Campo = C;
- Valor = V;
- }
- public CPValor(String C)
- {
- Campo = C;
- Valor = "";
- }
- public CPValor()
- {
- Campo = "";
- Valor = "";
- }
- public String GetCampo() {return Campo;}
- public String GetValor() {return Valor;}
- public void SetCampo(String C) {Campo = C;}
- public void SetValor(String V) {Valor = V;}
- }
- //Classe XMLObject (XMLObject.java)
- public class XMLObject
- {
- String nome;
- ArrayList<CPValor> LCamp = null;
- public XMLObject(String nom)
- {
- nome = nom;
- LCamp = new ArrayList<CPValor>();
- }
- public XMLObject()
- {
- nome = "";
- LCamp = new ArrayList<CPValor>();
- }
- void Add(CPValor P)
- {
- LCamp.add(P);
- }
- ArrayList<CPValor> GetCampos()
- {
- return LCamp;
- }
- String GetNome()
- {
- return nome;
- }
- String GetVal(String c)
- {
- for(int i=0; i<LCamp.size(); i++)
- {
- if(LCamp.get(i).GetCampo() == c)
- return LCamp.get(i).GetValor();
- }
- return "";
- }
- }
- //XMLParsing.java
- public class XMLParsing
- {
- private ArrayList<XMLObject> Obj = new ArrayList<>(); //The array of object we found on the xml
- public ArrayList<XMLObject> parse(InputStream in) throws XmlPullParserException, IOException
- {
- try
- {
- XmlPullParser parser = Xml.newPullParser(); //Pull parser to read the file
- parser.setInput(in, null); //we need to set the input to the file
- parser.nextTag(); //skip the first tag (that thing about the coding and stuff I believe)
- readFeed(parser); //Let's read the thing
- return Obj; //Now we can freely return the obj
- }
- finally
- {
- in.close(); //We always gotta close the file
- }
- }
- private void readFeed(XmlPullParser parser) throws XmlPullParserException, IOException
- {
- int num = 0; //the branch we are 1st, 2nd or 3rd...
- /*
- num 1 : Doc Start
- num 2 : Object start
- num 3 : Object info
- */
- int eventType = parser.getEventType(); //self explanatory afterwards
- //prevent "possibly not initialized" errors (facepalm) too protective...
- //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...
- CPValor n = new CPValor();
- XMLObject ob = new XMLObject();
- /*
- Adapt the following code according to your needs.
- Currently it only reads xml according to the following format:
- <Start>
- <Object>
- <Info>TEXT</info>
- </Object>
- </Start>
- if you got a info inside a info (a 4th branch as I call them) then that one will be read but not stored
- like this:
- <Start>
- <Object>
- <Info>
- <Info1>TEXT1</Info1>
- <Info2>TEXT2</Info2>
- <Info>
- <SInfo>TEXT3</SInfo>
- </Object>
- </Start>
- Object will be read and stored as a XMLObject name
- 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)
- Info1 and Info2 WILL be read BUT NOT stored anywhere...
- 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
- */
- while (eventType != XmlPullParser.END_DOCUMENT)
- {
- if(eventType == XmlPullParser.START_TAG) //start of a tag (<Start> for example)
- {
- num++; //start a tag then it's a new branch
- if(num == 2) //read above that huge comment
- ob.SetNome(parser.getName());
- if(num == 3)
- n.SetCampo(parser.getName());
- }
- else if(eventType == XmlPullParser.END_TAG) //end of a tag (</Start> for example)
- {
- //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
- if(num == 3) //bah...
- {
- ob.Add(n); //add the CPValor to the XMLObject
- n = new CPValor(); // create a new CPValor
- }
- if(num == 2)
- {
- Obj.add(ob); //add the XMLObject to the array
- ob = new XMLObject(); //Create a new XML
- }
- num--; //dah reduce a branch now
- }
- else if(eventType == XmlPullParser.TEXT) //If it's text then it will be the value for CPValue so put it there
- n.SetValor(parser.getText());
- eventType = parser.next(); //next thing
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment