XML 简单解析

XML simple parsing

我对解析 (Android Studio) 有疑问。这不是特别的事情。我的代码没有 运行。没有错误。我希望能够按下一个按钮并显示从 XML 文件解析的特定文本。

现在,我将省略按钮部分,并向您提供仅用于在简陋的 textview 上打印此文本的代码

XML 代码(app/src/main/assets 文件夹。articles.xml)

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE articles[
<!ELEMENT articles ANY>
<!ELEMENT article ANY>
<!ATTLIST article ID ID #IMPLIED> ]>
<articles>
 <article ID="a1">TEST
</article>
 <article ID="a2">1.TEST2
</article>

CLASS 代码

package com.blah.blah;

import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class MainActivity extends AppCompatActivity {

TextView textView;
String stringArticle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AssetManager assetManager = this.getAssets();
    InputStream inputStream = null;

     try {

         // Loads your XML file as an InputStream
         inputStream = assetManager.open("articles");

         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document doc = builder.parse(inputStream);

         Element article = doc.getElementById("a1");
         String stringArticle = article.getTextContent();

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(stringArticle);

     } catch (IOException e) {
         // Exception Handling Code
         e.printStackTrace();
     }catch (ParserConfigurationException e) {
         e.printStackTrace();
     } catch (SAXException e) {
         e.printStackTrace();
     }
}
}        

我还没有尝试过 return 你是节点列表的技术,因为我不想迭代任何东西。我发现它对我的项目没用。我想要的是极其原始的东西。就像那些简单的圣经应用程序。你按下带有诗句的按钮,相应的文本就会弹出。就这么简单。

再一次,完全没有错误!只是不工作。我认为 TRY 部分没有执行。因为它为我提供了 TextView 上的默认 'hello world' 文本。但是,一旦我把

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(stringarticle);
在 TRY/CATCH 块之后的

行,我只得到一个空的 TextView。

太!

您可以将 XML 文件放在 "app/src/main/assets" 目录中。之后,您可以通过 "AssetManager" class.

轻松访问 XML 文件

您可以使用以下代码作为参考:-

 AssetManager assetManager = this.getAssets();
    InputStream inputStream = null;
    try {
        // Loads your XML file as an InputStream
        inputStream = assetManager.open("articles.xml");

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputStream);
        Element article = doc.getElementById("1");
        String stringarticle = article.getTextContent();

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(stringarticle);

    } catch (IOException e) {
        // Exception Handling Code
        e.printStackTrace();
    }catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }

请记住将您的文件放在上述目录中,否则 "AssetManager" class 将无法读取它。

干杯!!