Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中解析xml文件,获取给定节点的所有属性_Java_Android_Xml_Dom_Sax - Fatal编程技术网

Java 在android中解析xml文件,获取给定节点的所有属性

Java 在android中解析xml文件,获取给定节点的所有属性,java,android,xml,dom,sax,Java,Android,Xml,Dom,Sax,我正试图从下面发布的xml文件中获取id、name和pin,我编写了下面发布的代码。 问题是,此行node.getNodeValue()始终返回null 请告诉我如何正确地从xml文件中获取id、name和pin 代码 try { URL url = new URL(link); URLConnection conn = url.openConnection(); DocumentBuilderFactory factor

我正试图从下面发布的
xml
文件中获取
id
name
pin
,我编写了下面发布的代码。 问题是,此行
node.getNodeValue()
始终返回null

请告诉我如何正确地从
xml
文件中获取
id
name
pin

代码

try {
            URL url = new URL(link);
            URLConnection conn = url.openConnection();

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

            doc.getDocumentElement().normalize();

            NodeList nodes = doc.getElementsByTagName("employee");

            Log.i(TAG, " nodeLength:" + nodes.getLength());


            for (int i = 0; i < nodes.getLength(); i ++) {
                Node node = nodes.item(i);
                Log.i(TAG, "Current Element :" + node.getNodeValue());//THIS LINE
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
01-24 15:05:54.219 5624-5718/com.example. I/XmlParser:  nodeLength:33
01-24 15:05:54.220 5624-5718/com.example. I/XmlParser: Current Element :null
01-24 15:05:54.220 5624-5718/com.example. I/XmlParser: Current Element :null
01-24 15:05:54.220 5624-5718/com.example. I/XmlParser: Current Element :null
01-24 15:05:54.220 5624-5718/com.example.I/XmlParser: Current Element :null
01-24 15:05:54.220 5624-5718/com.example. I/XmlParser: Current Element :null

您可以获取所有
属性的
NamedNodeMap
,并使用
getNamedItem
Id、pin、name
作为此函数的键,最后使用
getNodeValue
函数获取值

     NodeList nodes = doc.getElementsByTagName("employee");
     for (int i = 0; i < nodes.getLength(); i ++) {
         Node node = nodes.item(i);
         NamedNodeMap attr =  node.getAttributes();

         // find item using key name and then fetch the node value
         String id = attr.getNamedItem("id").getNodeValue();
         String pin = attr.getNamedItem("pin").getNodeValue();
         String name = attr.getNamedItem("name").getNodeValue();    
         System.out.println(id+" "+pin+" "+name);
     }
或者也可以使用循环,而不是获取单个值

     NodeList nodes = doc.getElementsByTagName("employee");
     for (int i = 0; i < nodes.getLength(); i ++) {
         Node node = nodes.item(i);
         NamedNodeMap attr =  node.getAttributes();
         for(int j = 0 ; j<attr.getLength() ; j++) {
             Attr attribute = (Attr)attr.item(j);     
             System.out.println("Current Element :" + attribute.getName()+" = "+attribute.getValue());
          } 
     }
NodeList nodes=doc.getElementsByTagName(“员工”);
对于(int i=0;i对于(int j=0;jpoststacktrace@Rasi发布请查看此
节点。getAttributes().getNamedItem(“id”).getNodeValue()
如何获取子节点及其属性值,如果我有如下xml:@JerryAbraham use
((元素)doc.getElementsByTagName(“结果”).item(0)).getElementsByTagName(“字段”);
这将为您提供
节点列表
对象,因此使用循环(如上面的答案所示)并遍历它,同时应用对象强制转换来获取属性
21358 0000 www
21359 0011 qqq
20752 1011 Test
814 1012 Pf
21372 1013 Kru
     NodeList nodes = doc.getElementsByTagName("employee");
     for (int i = 0; i < nodes.getLength(); i ++) {
         Node node = nodes.item(i);
         NamedNodeMap attr =  node.getAttributes();
         for(int j = 0 ; j<attr.getLength() ; j++) {
             Attr attribute = (Attr)attr.item(j);     
             System.out.println("Current Element :" + attribute.getName()+" = "+attribute.getValue());
          } 
     }