简体   繁体   English

从本地文件而不是URL解析XML

[英]Parse XML from local file instead from URL

so after your guides i changed the code to this but the app just hangs... whats the next step from here? 所以在您的指南之后,我将代码更改为此,但是该应用程序只是挂起了...接下来的下一步是什么? getXML is called from 从调用getXML

Document xml = XMLemailF.getXML();

.

    public static Document getXML(){     
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    // get a document builder
    DocumentBuilder db = null;
    try {
        db = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //parse using builder to get DOM representation of the XML file
    Document doc = null;
    try {
        doc = db.parse("/xxxxxx/res/xml/email.xml");
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return doc;

}

this is called 这就是所谓的

public static int numResults(Document doc){     
    Node results = doc.getDocumentElement();
    int res = -1;

    try{
        res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
    }catch(Exception e ){
        res = -1;
    }

    return res;
}

through this 通过这个

...
Document xml = XMLemailF.getXML();

    int numResults = XMLemailF.numResults(xml);

    if((numResults <= 0)){
        Toast.makeText(contactus_email.this, "No results found", Toast.LENGTH_LONG).show();  
        finish();
    }

    NodeList nodes = xml.getElementsByTagName("result");

    for (int i = 0; i < nodes.getLength(); i++) {                           
        HashMap<String, String> map = new HashMap<String, String>();    

        Element e = (Element)nodes.item(i);
        map.put("email", XMLemailF.getValue(e, "email"));
        map.put("line1", XMLemailF.getValue(e, "line1"));
        mylist.add(map);
...

Any ideas? 有任何想法吗?

Do you need to get xml DOM from a local file? 您是否需要从本地文件获取xml DOM? Do I understand right? 我明白吗?

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new FileInputStream("/path/to/your/file.xml");

Or 要么

Document doc = builder.parse(getClass().getResourceAsStream("/path/to/your/resource.xml"));

Of course you can 当然可以

try something like: 尝试类似的东西:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

// get a document builder
DocumentBuilder db = dbf.newDocumentBuilder();

//parse using builder to get DOM representation of the XML file
Document doc = db.parse("yourFile.xml");

and look in the javadoc of these classes for more infos 并查看这些类的javadoc以获取更多信息

Here is method that uses SAXPArser to read xml file on sdcard 这是使用SAXPArser读取sdcard上的xml文件的方法

public static RSSFeed parseLocalXml(String filename){
    try {



        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);

        SAXParser parser = factory.newSAXParser();

        XMLReader xmlreader = parser.getXMLReader();

        RSSHandler theRssHandler = new RSSHandler();

        xmlreader.setContentHandler(theRssHandler);

        InputSource is = new InputSource(new FileInputStream(filename));

        xmlreader.parse(is);

        return theRssHandler.getFeed();//this is handler that reads trough xml nodes
    } catch (Exception ee) {

        return null;
    }
}

use application.getResourceAsStream("your file name") like this: 像这样使用application.getResourceAsStream(“ your file name”)

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(application.getResourceAsStream("file.xml"));

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

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