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

标签 java android xml dom sax

我正在尝试从下面发布的 xml 文件中获取 idnamepin 并且我写了下面发布的代码。 问题是,这一行 node.getNodeValue() 总是返回 null

请告诉我如何从 xml 文件中正确获取 idnamepin

代码

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

xml

<employee_list>
<employee id="21358" pin="0000" name="www"/>
<employee id="21359" pin="0011" name="qqq"/>
<employee id="20752" pin="1011" name="Test"/>
<employee id="814" pin="1012" name="Pf"/>
<employee id="21372" pin="1013" name="Kru"/>
</employee_list>

堆栈跟踪

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

最佳答案

您可以获取所有 AttributesNamedNodeMap 并使用 getNamedItemId,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);
     }

输出:

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

关于java - 解析android中的xml文件,获取给定节点的所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41830327/

相关文章:

android - RecyclerView android 中的 ScrollView

android - 有没有一种方法可以设置 build.gradle 中的 buildConfigField 值以直接在布局 xml 文件中查看?

java - 如何检索实际数据标签中的文本?

java - 如何使用 CMIS 删除文档

java - 我可以使用 Izpack 5 安装文件而不将其作为 izpack 构建的 install.jar 的一部分吗

java - 传递 null 时将在重载期间调用哪个方法

java - 计算两个瞬间之间的天数、小时数和分钟数

android - 将 float[] 转换为 android.graphics.Matrix

android - FMXBroadcastReceiver (Android) 在 Delphi 10 Seattle 中损坏了吗?自 XE7 以来发生了什么变化?

c# - 使用 XmlSerializer 反序列化 XML 时保留纯空白元素内容