大家好,

我是Android新手。我正在使用DOM解析来读取xml字符串值。为此,我使用了以下代码,该代码将在给出异常后获取根元素值,请解决此问题,

预先感谢,

Xml代码:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<ChangePassword>
  <Oldpassword>23545565635354</Oldpassword>
  <Newpassword>addsffggfdsfdsfdfs </Newpassword>
</ChangePassword>


Java代码:

   File file = new File(getFilesDir().getAbsolutePath()+ File.separator + "test.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                      DocumentBuilder db = dbf.newDocumentBuilder();
                      Document doc = db.parse(file);
                      doc.getDocumentElement().normalize();
                      System.out.println("Root element " + doc.getDocumentElement().getNodeName());
                      NodeList nodeLst = doc.getElementsByTagName("ChangePassword");
                      System.out.println("Information of all entries");

                      for (int s = 0; s < nodeLst.getLength(); s++) {

                        Node fstNode = nodeLst.item(s);

                        if (fstNode.getNodeType() == Node.ELEMENT_NODE)
                        {
                          Element fstElmnt = (Element) fstNode;

                          // Firstname
                          NodeList fstNmElmntLst = ((Document) fstElmnt).getElementsByTagName("Oldpassword");
                          Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                          NodeList fstNm = ((Node) fstNmElmnt).getChildNodes();
                          System.out.println("Old password : "  + ((Node) fstNm.item(0)).getNodeValue());

                          // Lastname
                          NodeList lstNmElmntLst = ((Document) fstElmnt).getElementsByTagName("Newpassword");
                          Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
                          NodeList lstNm = ((Node) lstNmElmnt).getChildNodes();
                          System.out.println("Old password : " + ((Node) lstNm.item(0)).getNodeValue());

                          // Address
                          NodeList addrNmElmntLst = ((Document) fstElmnt).getElementsByTagName("Newpassword");
                          Element addrNmElmnt = (Element) addrNmElmntLst.item(0);
                          NodeList addrNm = ((Node) addrNmElmnt).getChildNodes();
                          System.out.println("Address : " + ((Node) addrNm.item(0)).getNodeValue());
                        }
                      }
                  } catch (Exception e) {
                      Log.e("Exception",e.toString());
                    //e.printStackTrace();
                  }

最佳答案

Document doc = db.parse(in);
Element docElem = doc.getDocumentElement();
NodeList nl = docElem.getElementsByTagName("Oldpassword");


试试...

更新
如果您在这里看看,可能会有所帮助:http://www.w3schools.com/xml/default.asp

以下代码正在工作,刚刚经过测试。

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class testxml {

private String filepath = "src/xml.xml";

public void parse() {

    File file = new File(filepath);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    try {
        db = dbf.newDocumentBuilder();
        Document doc = db.parse(file);

        Element docElem = doc.getDocumentElement();
        NodeList nl1 = docElem.getElementsByTagName("Oldpassword");

        for(int i = 0; i < nl1.getLength(); i++) {
            Element entry = (Element)nl1.item(i);
            System.out.println(entry.getFirstChild().getNodeValue());
        }

        NodeList nl2 = docElem.getElementsByTagName("Newpassword");

        for(int i = 0; i < nl2.getLength(); i++) {
            Element entry = (Element)nl2.item(i);
            System.out.println(entry.getFirstChild().getNodeValue());
        }

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main(String args[]) {

    testxml x = new testxml();
    x.parse();

}


}

关于java - 如代码所示,如​​何使用此路径读取xml字符串值(getFilesDir()。getAbsolutePath()+ File.separator +“test.xml”),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6290926/

10-17 01:20