如何连接解析dom和url?

时间:2019-02-16 15:43:54

标签: android-parser

伙计们! Java DOM解析器有一个小问题。 我到底是什么意思 例如,我的网址带有xml代码:

<players>
    <player>
        <firstname> Tomas</firstname>
        <age>33</age>
        <secondname>Otto</secondname>
    </player>
    <player>
        <firstname>Tifany</firstname>
        <age>15</age>
        <secondname>Azar</secondname>
    </player>
    <player>
        <firstname> Ivan</firstname>
        <age>42</age>
        <secondname>Nowak</secondname>
    </player>
</players>

在内部,您可以检查我已经做了什么,但这不是我想要的。 我不想仅从url的xml中从文件的.xml中获取文本

我知道如何与xml连接。文件(如果该文件已经存在于我的AndroidStudio中)。

package com.example.okhttp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.TextView;

import com.android.volley.RequestQueue;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {
    private TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = findViewById(R.id.textView);
        parseXML();

    }

    private void parseXML() {
        try {
            XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = parserFactory.newPullParser();
            InputStream is = getAssets().open("data.xml");
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(is, null);
            processParsing(parser);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void processParsing(XmlPullParser parser) throws IOException, XmlPullParserException {
        ArrayList<Player> players = new ArrayList<>();
        int evenType = parser.getEventType();
        Player currentPlayer = null;
        while (evenType != XmlPullParser.END_DOCUMENT){
            String eltName = null;
            switch(evenType){
                case XmlPullParser.START_TAG:
                eltName = parser.getName();
                if("player".equals(eltName)){
                    currentPlayer = new Player();
                    players.add(currentPlayer);
                }else if (currentPlayer != null){
                    if("firstname".equals(eltName)){
                        currentPlayer.firname = parser.nextText();
                    }else if ("age".equals(eltName)){
                        currentPlayer.age = parser.nextText();
                    }else if ("secondname".equals(eltName)){
                        currentPlayer.secondname = parser.nextText();
                    }
                }
                break;
            }
            evenType = parser.next();
        }
        printPlayers(players);
    }

    private void printPlayers(ArrayList<Player> players) {
        StringBuilder builder = new StringBuilder();

        for (Player player: players){
            builder.append(player.firname).append("\n")
                    .append(player.age).append("\n")
                    .append(player.secondname).append("\n\n");
        }
        txt.setText(builder.toString());
    }

}




package com.example.okhttp;

public class Player {
    public String firname, age, secondname;

}

我只是想知道我应该如何更改我的代码以从url获取所有数据

0 个答案:

没有答案