如何使用此路径读取xml字符串值,如代码所示(getFilesDir()。getAbsolutePath()+ File.separator +“test.xml”)

时间:2011-06-09 09:46:24

标签: java android xml

大家好,

我是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();
                  }

3 个答案:

答案 0 :(得分:2)

哇。 DOM Parser代码非常难看。请try Simple XML instead。看看你的代码是什么样的:

@Root(name = "ChangePassword")
public class PasswordChange {
   @Element(name = "Oldpassword")
   public String oldPassword;

   @Element(name = "Newpassword")
   public String newPassword;
}

这样更好。然后你可以说:

Serializer serial = new Persister();
PasswordChange pc = serial.read(PasswordChange.class, streamOrFileWithXML);

这就是它的全部内容。如果您想了解如何在Android中添加look at my blog post

答案 1 :(得分:0)

尝试更改此行

NodeList nodeLst = doc.getElementsByTagName("ChangePassword");

到此

NodeList nodeLst = doc.getDocumentElement().getElementsByTagName("ChangePassword");

如果没有,请告诉我们您的堆栈跟踪。

答案 2 :(得分:0)

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();

}

}