简体   繁体   English

如何将XML输入流解析为字符串或对象(Android Studio)

[英]How to parse XML inputstream into string or objects (Android studio)

Iam new with android and Java. Iam是Android和Java的新功能。 (I can program in C#) and iam trying to retrieve some XML file from the internet and parse it into objects. (我可以用C#编程),我试图从Internet检索一些XML文件并将其解析为对象。 Iam struggling with it for hours right now. Iam正在努力奋战几个小时。 I think it isn't very complicated but i cant find the right way to do it. 我认为这不是很复杂,但是我找不到正确的方法。

Iam started with this but there is a lot of stuff in it i don't need so i tried to write some clean code with only the things i need. Iam 从此开始,但是其中有很多东西我不需要,所以我尝试只用我需要的东西编写一些干净的代码。 I read a lot of articles on the internet but i cant get them working.... 我在互联网上阅读了很多文章,但我无法使它们工作。

I created an default project in android studio with one Activity. 我在Android Studio中使用一个Activity创建了一个默认项目。 I created an subclass in this activity caled XmlManager. 我在此活动中创建了一个名为XmlManager的子类。 This subclass extends the AsyncTask. 该子类扩展了AsyncTask。 This are the methods in it: 这是其中的方法:

private class XmlManager extends AsyncTask<String,String,String> {
@Override
protected String doInBackground(String... inputUrl)
{
    Log.i("XML Status", "ASync proces started");
    getStream(inputUrl[0]);
    Log.i("XML Status", "ASync proces finished");
    return null;
}

@Override
protected void onPostExecute(String result)
{
    //Here i want to return the list with objects...
    // OR an string with the XML so i can parse it somewhere else,
    // I dont know what is the best practice
}

private void getStream(String urlString) {

    URL url =null;
    HttpURLConnection urlConnection = null;
    InputStream is = null;

    try {
        url = new URL(urlString);
        Log.i("Status", "Connection opened");
        urlConnection  = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        is = urlConnection.getInputStream();
        Log.i("Status", "Streamreader filled");
    }
    catch (Exception e) {
        Log.e("Error",e.getMessage().toString());
    }
    finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
            Log.i("Status","Connection disconnected");
        }
    }
}

Can someone help me please? 有人能帮助我吗? or have an link to an WORKING and EASY example(i cant find :'( ) 或链接到工作轻松的示例(我找不到:'()

I read something about a Simple XML library for parsing XML to objects, is this the way to do it? 我读过一些有关将XML解析为对象的简单XML库的信息,这是这样做的方法吗? try to retrieve XML as string or something and then parse it with any library? 尝试将XML检索为字符串或其他内容,然后将其与任何库一起解析?

Thanks in advance! 提前致谢!

you can parse xml to java object by JAXB. 您可以通过JAXB将xml解析为java对象。

1) you can change xml to xsd by xmlspy. 1)您可以通过xmlspy将xml更改为xsd。

2) using eclipse plugins( http://sourceforge.net/projects/jaxb-builder/ ) to generate java class . 2)使用eclipse插件( http://sourceforge.net/projects/jaxb-builder/ )生成java类。

3) use jaxb to parse xml to java object. 3)使用jaxb将xml解析为java对象。 the example code like this: 示例代码如下:

 JAXBContext context = JAXBContext.newInstance(XXXX.class);
 Unmarshaller unmarshaller = context.createUnmarshaller();
 XXXX obj = unmarshaller.unmarshal(is);

XXXX is the second step generate java class. XXXX是第二步生成Java类。

try to use SitesXmlPullParser from SrackSites here is the link: https://github.com/FoamyGuy/StackSites/blob/master/src/com/makemyandroidapp/example/stacksites/SitesXmlPullParser.java 尝试从SrackSites使用SitesXmlPullParser,这里是链接: https : //github.com/FoamyGuy/StackSites/blob/master/src/com/makemyandroidapp/example/stacksites/SitesXmlPullParser.java

the idea is to get the tags to every object so that you can choose whatever you want to use and put the items in a list 想法是将标签获取到每个对象,以便您可以选择要使用的任何对象并将其放在列表中

I have found what i missed. 我找到了我想念的东西。 I needed and bufferedReader and an Stringbuilder for making an string from the XML from the website. 我需要用bufferedReader和Stringbuilder来从网站的XML中生成字符串。 No i can parse the string into and object with the SimpleXML library. 不,我无法将字符串解析为SimpleXML库并成为对象。 The getStream method what i now use is: 我现在使用的getStream方法是:

   private void getStream(String urlString) {
        URL url = null;
        HttpURLConnection urlConnection = null;
        InputStream is = null;

        try {
            url = new URL(urlString);

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();
            Log.i("Status", "Connection opened");

            BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line);
            }
            Log.i("Status", "XML loaded into string");

        } catch (Exception e) {
            Log.e("Error", e.getMessage().toString());
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
                Log.i("Status", "Connection disconnected");
            }
        }
    }

The string "total" contains the XML code. 字符串“ total”包含XML代码。 Thanks for the help anyway. 无论如何,谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM