0

I need to parse a simple xml file. Here is an example of first rank child:

<group>
    <word>he_is</word>
    <english>&#60;strong&#62;Personal pronouns subject and the verb "to be": he is&#60;&#47;strong&#62;&#60;br&#62;1) personal pronouns subject:&#60;br&#62;I&#60;br&#62;you&#60;br&#62;he, she, it&#60;br&#62;we&#60;br&#62;you&#60;br&#62;they&#60;br&#62;The personal pronoun at the first person singular, I, is always written with an uppercase.&#60;br&#62;Ordinary English uses the same pronoun for the second person singular and the second person plural: &#60;strong&#62;you&#60;/strong&#62;.&#60;br&#62;2) conjugation of the verb &#60;strong&#62;to be&#60;/strong&#62;:&#60;br&#62;I am&#60;br&#62;you are&#60;br&#62;he, she, it is&#60;br&#62;we are&#60;br&#62;you are&#60;br&#62;they are&#60;br&#62;&#60;strong&#62;I am&#60;&#47;strong&#62; is often written and pronounced in its contracted form, &#60;strong&#62;I'm&#60;&#47;strong&#62;. So is &#60;strong&#62;he is&#60;&#47;strong&#62; &#8594; &#60;strong&#62;he's&#60;&#47;strong&#62;.</english>
    <francais>&#60;strong&#62;Les pronoms personnels sujets et le verbe "to be"&#60;&#47;strong&#62; &#60;i&#62;être&#60;&#47;i&#62;: &#60;strong&#62;he is&#60;&#47;strong&#62;&#60;br&#62;1) les pronoms personnels sujets:&#60;br&#62;I, je&#60;br&#62;you, tu&#60;br&#62;he/she/it, il/elle/pronom neutre&#60;br&#62;we, nous&#60;br&#62;you, vous&#60;br&#62;they, ils/elles&#60;br&#62;Le pronom personnel de la première personne du singulier, I, est toujours écrit en majuscule, même au milieu d'une phrase.&#60;br&#62;L'anglais courant ne fait pas de distinction entre tu et vous. Dans les deux cas, le pronom est &#60;strong&#62;you&#60;/strong&#62;.&#60;br&#62;2) conjugaison du verbe &#60;strong&#62;to be&#60;/strong&#62;, &#60;i&#62;être&#60;/i&#62;:&#60;br&#62;I am, je suis&#60;br&#62;you are, tu es&#60;br&#62;he/she/it is, il/elle/neutre est&#60;br&#62;we are, nous sommes&#60;br&#62;you are, vous êtes&#60;br&#62;they are ils/elles sont&#60;br&#62;Hormis à la première et la troisième personne du singulier, la forme unique est &#60;strong&#62;are&#60;/strong&#62;.&#60;br&#62;&#60;strong&#62;I am&#60;&#47;strong&#62;, &#60;i&#62;je suis&#60;&#47;i&#62;, est souvent écrit et prononcé sous sa forme contractée, &#60;strong&#62;I'm&#60;&#47;strong&#62;. De même &#60;strong&#62;he is&#60;&#47;strong&#62; est souvent écrit et prononcé &#60;strong&#62;he's&#60;&#47;strong&#62;.</francais>
    <espanol>&#60;strong&#62;Pronombres sujetos y el verbo "to be"&#60;&#47;strong&#62; &#60;i&#62;ser, estar&#60;&#47;i&#62;: &#60;strong&#62;he is&#60;&#47;strong&#62;&#60;br&#62;1) pronombres sujetos:&#60;br&#62;I, yo&#60;br&#62;you, t&#250;/usted&#60;br&#62;he/she/it, él/ella/pronombre neutro&#60;br&#62;we, nosostros&#60;br&#62;you, vosotros/ustedes&#60;br&#62;they, ellos/ellas&#60;br&#62;El pronombre personal de la primera persona del singular, I, siempre se escribe en mayúscula, incluso en medio de una frase.&#60;br&#62;No hay una distinción entre tú y vosotros. En cualquier caso, el pronombre es &#60;strong&#62;you&#60;/strong&#62;.&#60;br&#62;2) conjugación del verbo &#60;strong&#62;to be&#60;/strong&#62;, &#60;i&#62;ser / estar&#60;/i&#62;:&#60;br&#62;I am, yo soy/estoy&#60;br&#62;you are, tú/usted eres/estás/es/está&#60;br&#62;he/she/it is, él/ella/neutro es/está&#60;br&#62;we are, nosotros somos/estamos&#60;br&#62;you are, vosotros/ustedes sois/estáis/son/están&#60;br&#62;they are ellos/ellas son/están&#60;br&#62;A excepción de la primera y la tercera persona del singular, la forma única es &#60;strong&#62;are&#60;/strong&#62;.&#60;br&#62;&#60;strong&#62;I am&#60;&#47;strong&#62;, &#60;i&#62;yo soy&#60;&#47;i&#62;, a menudo se escribe y se pronuncia en su forma contraída, &#60;strong&#62;I'm&#60;&#47;strong&#62;. También &#60;strong&#62;he is&#60;&#47;strong&#62; &#8594; &#60;strong&#62;he's&#60;&#47;strong&#62;.</espanol>
    <chinois>&#60;strong&#62;人称代词主格及动词“to be”:he is&#60;&#47;strong&#62;&#60;br&#62;1) 人称代词主格&#60;br&#62;I, 我&#60;br&#62;you, 你&#60;br&#62;he, she, it, 他,她,它&#60;br&#62;we, 我们&#60;br&#62;you, 你们&#60;br&#62;they, 他们,她们,它们&#60;br&#62;第一人称单数人称代词,我,通常以大写形式书写&#60;br&#62;日常英语中第二人称单数和复数使用同样的代词:you。&#60;br&#62;2) 动词&#60;strong&#62;to be&#60;/strong&#62;的词形变化:&#60;br&#62;I am,  我 是&#60;br&#62;you are  你   是&#60;br&#62;he/she/it  is  他/她/它   是&#60;br&#62;we are 我们 是&#60;br&#62;you are 你们 是&#60;br&#62;they are 他们 是&#60;br&#62;“&#60;strong&#62;I am&#60;&#47;strong&#62;(我是)”通常在书写和发音上使用缩写形式,&#60;strong&#62;I’m&#60;&#47;strong&#62;。&#60;strong&#62;He is&#60;&#47;strong&#62; 也是一样 &#8594; &#60;strong&#62;he's&#60;&#47;strong&#62;。</chinois>
</group>

I undestand that there are several ways: SAX, XmlPullParser, DOM, and others. And SAX comes in different flavors.

Having read someplace that XmlPullParser is the simplest, that's what I use in the following makeshift way

public void lireXML(String mot)
        throws XmlPullParserException, IOException
{

    final TextView monMessage = (TextView) findViewById(R.id.zone_trado_scrollable);

    XmlResourceParser monFichierXML = getResources().getXml(R.xml.grammar);

    monFichierXML.next();

    int eventType = monFichierXML.getEventType();

    while (eventType != XmlPullParser.END_DOCUMENT)
    {
        if( (eventType == XmlPullParser.START_TAG) && (monFichierXML.getName().equalsIgnoreCase("word"))  )
        {
            monFichierXML.next();
            if ( monFichierXML.getText().equalsIgnoreCase(mot) )
            {

                monFichierXML.nextTag(); // word fermant
                monFichierXML.nextTag(); // english ouvrant
                monFichierXML.next();    // texte anglais

                if (TextActivity.langueChoisie == "francais") {
                    monFichierXML.nextTag(); // english fermant
                    monFichierXML.nextTag(); // français ouvrant
                    monFichierXML.next();    // texte français
                }

                if (TextActivity.langueChoisie == "espanol") {
                    monFichierXML.nextTag(); // english fermant
                    monFichierXML.nextTag(); // français ouvrant
                    monFichierXML.next();    // texte français
                    monFichierXML.nextTag(); // français fermant
                    monFichierXML.nextTag(); // espanol ouvrant
                    monFichierXML.next();    // texte espanol
                }

                if (TextActivity.langueChoisie == "chinois") {
                    monFichierXML.nextTag(); // english fermant
                    monFichierXML.nextTag(); // français ouvrant
                    monFichierXML.next();    // texte français
                    monFichierXML.nextTag(); // français fermant
                    monFichierXML.nextTag(); // espanol ouvrant
                    monFichierXML.next();    // texte espanol
                    monFichierXML.nextTag(); // espagnol fermant
                    //monFichierXML.nextTag(); // russe ouvrant
                    //monFichierXML.next();    // texte russe
                    //monFichierXML.nextTag(); // russe fermant
                    monFichierXML.nextTag(); // chinois ouvrant
                    monFichierXML.next();    // texte chinois
                }

                // on affiche le texte à l'intérieur de la balise sélectionnée
                //monMessage.setText(monFichierXML.getText());
                monMessage.setText(Html.fromHtml(monFichierXML.getText()));
            }
        }
        eventType = monFichierXML.next();
    }
}

It works, but I suspect I'm not using the best way to parse my xml file. Any suggestion?

Andy
  • 99
  • 1
  • 2
  • 9

1 Answers1

1

It is perfectly correct to use XMLPullParser to parse it, maybe your code is not as perfect, but as long as you implement it well there is no problem with using it.

Almost all my xml parsers did end up looking really similar to this

try{
    XmlPullParser parser = Xml.newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    parser.setInput(this.getAssets().open("xml/" + source), null);

    while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {

        if (parser.getEventType() == XmlPullParser.START_TAG
                && parser.getName().compareTo("map") == 0) {

            for(int c=0;c<parser.getAttributeCount();c++)
            {
                if(parser.getAttributeName(c).compareTo("title")==0)
                {
                    title = parser.getAttributeValue(c);
                }

                if(parser.getAttributeName(c).compareTo("bcolor")==0)
                {
                    bColor = Color.parseColor(parser.getAttributeValue(c));
                }

                if(parser.getAttributeName(c).compareTo("tcolor")==0)
                {
                    tColor = Color.parseColor(parser.getAttributeValue(c));
                }
            }
        }

        if (parser.getEventType() == XmlPullParser.START_TAG
                && parser.getName().compareTo("zone") == 0) {

            int x1=0,x2=0,y1=0,y2=0;
            String data="",action="";

            for(int c=0;c<parser.getAttributeCount();c++)
            {
                if(parser.getAttributeName(c).compareTo("x1")==0)
                {
                    x1 = Integer.parseInt(parser.getAttributeValue(c));
                }

                if(parser.getAttributeName(c).compareTo("y1")==0)
                {
                    y1 = Integer.parseInt(parser.getAttributeValue(c));
                }

                if(parser.getAttributeName(c).compareTo("x2")==0)
                {
                    x2 = Integer.parseInt(parser.getAttributeValue(c));
                }

                if(parser.getAttributeName(c).compareTo("y2")==0)
                {
                    y2 = Integer.parseInt(parser.getAttributeValue(c));
                }

                if(parser.getAttributeName(c).compareTo("action")==0)
                {
                    action = parser.getAttributeValue(c);
                }

                if(parser.getAttributeName(c).compareTo("data")==0)
                {
                    data = parser.getAttributeValue(c);
                }
            }

            zonas.add(new Zone(x1,x2,y1,y2,action,data));
        }
        parser.next();
    }

Explained:

While loop, end condition: parser.getEventType() != XmlPullParser.END_DOCUMENT

If statement for each different Tag you want to parse, 2 Conditions, it must be START_TAG and the name Strings must match.

If you need to parse tag attributes make a for loop to getAttributeCount(), never rely on attributes order.

Maybe you need to perform something when reach a END_TAG, similar condition as before but changing START with END

And finally, the last line of the while must be parser.next(); so it will go to the next tag of the xml.

Hope this helps.

Nanoc
  • 2,381
  • 1
  • 20
  • 35