0

我有一个本地 xml 文件。我在这里存储:res/xml/myxml.xml

<?xml version="1.0" encoding="utf-8"?>
<list>
<lists>
    <tag>College Website</tag>
    <value>http://www.nitk.ac.in/</value>
    <lastupdate>04-07-2012</lastupdate>
</lists>
<lists>
    <tag>College Images</tag>
    <value>http://studentsworld.nitk.ac.in/index.php?q=convocation.html</value>
    <lastupdate>25-9-2011</lastupdate>
</lists>
</list>

我有循环文档。但是如何在文档解析器中连接本地 xml 文件。我不想要 url 模型,我想要一个本地 xml 文件。

for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            tag[i] = new TextView(this);
            value[i] = new TextView(this);
            lastupdate[i] = new TextView(this);

            Element fstElmnt = (Element) node;
            NodeList nameList = fstElmnt.getElementsByTagName("tag");
            Element nameElement = (Element) nameList.item(0);
            nameList = nameElement.getChildNodes();
            tag[i].setText("tag = " + ((Node) nameList.item(0)).getNodeValue());

            NodeList websiteList = fstElmnt.getElementsByTagName("value");
            Element websiteElement = (Element) websiteList.item(0);
            websiteList = websiteElement.getChildNodes();
            value[i].setText("value = " + ((Node) websiteList.item(0)).getNodeValue());

            NodeList lastupdateList = fstElmnt.getElementsByTagName("tag");
            Element lastupdateElement = (Element) lastupdateList.item(0);
            lastupdateList = lastupdateElement.getChildNodes();
            lastupdate[i].setText("lastupdate = " + ((Node) lastupdateList.item(0)).getNodeValue());



            layout.addView(tag[i]);
            layout.addView(value[i]);
            layout.addView(lastupdate[i]);

        }
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

我有 main.xml 文件,请给我一个代码

4

3 回答 3

2

把你的 XML 文件放在 assets 文件夹中,然后试试这个

InputStream raw = getApplicationContext().getAssets().open("simple.xml");

试试这个

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder= dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(raw);
....
...
于 2012-07-06T06:43:46.983 回答
1

将您的 XML 文件放在assets文件夹中并尝试这种方式

 String xml;
 Document doc;
 try{
 xml=getXML(getAssets().open("yourxmlfilename.xml"));
 }catch(Exception e){
     Log.d("Error",e.toString());
  }
 doc = XMLfromString(xml);

其中getXMLXMLfromString方法如下图

 //getXML   method
 public static String getXML(InputStream is)throws IOException {

    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result = bis.read();
    while(result != -1) {
      byte b = (byte)result;
      buf.write(b);
      result = bis.read();
    }        
    return buf.toString();
}


   //XMLfromString   method
  public final static Document XMLfromString(String xml){
     Document doc = null;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            try {

          DocumentBuilder db = dbf.newDocumentBuilder();

          InputSource is = new InputSource();
              is.setCharacterStream(new StringReader(xml));
              doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
          System.out.println("XML parse error: " + e.getMessage());
          return null;
        } catch (SAXException e) {
          System.out.println("Wrong XML file structure: " + e.getMessage());
                return null;
        } catch (IOException e) {
          System.out.println("I/O exeption: " + e.getMessage());
          return null;
        }

            return doc;

  }
于 2012-07-06T07:00:48.000 回答
0

请使用以下代码解析本地 xml,

InputStream in = this.getResources().openRawResource(R.raw.xmlfile);

然后解析输入流,

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
DocumentBuilder db = dbf.newDocumentBuilder ();
Document document = db.parse(in, null);
于 2012-07-06T06:48:49.797 回答