Java 如何在xml解析中解析项和子项 \\192.168.1.102\CMS\DATA \通知\DIRECTTAXLAWS\HTMLFILES\150025021978.htm 2187[S.O.1500]| 1961年《所得税法》第35(1)(ii)节-科研支出-经批准的科研协会/机构 对 20120328160152743

Java 如何在xml解析中解析项和子项 \\192.168.1.102\CMS\DATA \通知\DIRECTTAXLAWS\HTMLFILES\150025021978.htm 2187[S.O.1500]| 1961年《所得税法》第35(1)(ii)节-科研支出-经批准的科研协会/机构 对 20120328160152743,java,android,Java,Android,代码: ArrayList menuItems=new ArrayList(); XMLParser=新的XMLParser(); 字符串xml=parser.getXmlFromUrl(URL);//获取XML Document doc=parser.getdoElement(xml);//获取DOM元素 NodeList nl=doc.getElementsByTagName(KEY_DOCDETAILS); //循环通过所有项目节点 对于(int i=0;i

代码:

ArrayList menuItems=new ArrayList();
XMLParser=新的XMLParser();
字符串xml=parser.getXmlFromUrl(URL);//获取XML
Document doc=parser.getdoElement(xml);//获取DOM元素
NodeList nl=doc.getElementsByTagName(KEY_DOCDETAILS);
//循环通过所有项目节点
对于(int i=0;i
这是我的Xml格式,我想xmlParse,想显示id,日期,标题,我可以显示标题,但我不能打印日期和id,请告诉我如何实现它。这是我打印标题的代码。请修改我的代码并打印标题、id、日期。

NodeList nl=doc.getElementsByTagName(“信息”);
  ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_DOCDETAILS);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            map.put(KEY_HEADING, parser.getValue(e, KEY_HEADING));

            // adding HashList to ArrayList
            menuItems.add(map);


       }
对于(int i=0;i
1.声明

     NodeList nl = doc.getElementsByTagName("info");

            for (int i = 0; i < nl.getLength(); i++) {

                Element e = (Element) nl.item(i);
     NamedNodeMap attributes = e.getAttributes();

        for (int a = 0; a < attributes.getLength(); a++) 
        {
                Node theAttribute = attributes.item(a);
                System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
        }
}
2.解析

      String URL = "http://www.google.co.in/ig/api?news&hl=en";
      String KEY_ITEM = "news"; 
      String KEY_ID = "news_entry";
      String KEY_NAME = "title";
      String KEY_COST = "url";
      String KEY_DESC = "snippet";
XMLParser=newxmlparser();
字符串xml=parser.getXmlFromUrl(URL);//获取XML
Document doc=parser.getdoElement(xml);//获取DOM元素
NodeList nl=doc.getElementsByTagName(键项);
对于(int i=0;i对于(int j=0;jp请看以下内容:你能给我添加逻辑代码吗?因为我正在努力,但无法做到这一点。我已经做了这个示例,但无法理解这是我的Url。请现在告诉我。对不起,我无法解析你的xml,因为它给出的响应代码为500,我想它受到服务器的一些限制。但我已经为你解析了其他xml,这就满足了l您的需求。试试看,这是对其他xml的解析。
      String URL = "http://www.google.co.in/ig/api?news&hl=en";
      String KEY_ITEM = "news"; 
      String KEY_ID = "news_entry";
      String KEY_NAME = "title";
      String KEY_COST = "url";
      String KEY_DESC = "snippet";
 XMLParser parser = new XMLParser();
 String xml = parser.getXmlFromUrl(URL); // getting XML
 Document doc = parser.getDomElement(xml); // getting DOM element
 NodeList nl = doc.getElementsByTagName(KEY_ITEM);
   for (int i = 0; i < nl.getLength(); i++) {

   HashMap<String, String> map = new HashMap<String, String>();
    Element e = (Element) nl.item(i);
     NamedNodeMap attributes = e.getAttributes();
    System.out.println("attrlength"+attributes.getLength());
     for (int a = 0; a < attributes.getLength(); a++) 
     {
             Node theAttribute = attributes.item(a);
             System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
     }
         NodeList nl1=e.getElementsByTagName(KEY_ID);
     System.out.println("keyId"+nl1.getLength());
     for(int j=0;j<nl1.getLength();j++)
     {
         Element e1 = (Element) nl1.item(j);

         NodeList n = e1.getElementsByTagName(KEY_NAME);

          for (int k = 0; k < n.getLength(); k++) {

              Element e2 = (Element) n.item(k);
        //  System.out.println("node Title value"+e2.getNodeName());
             NamedNodeMap attributes2 = e2.getAttributes();
            // System.out.println("attrlength"+attributes2.getLength());
             for (int a = 0; a < attributes2.getLength(); a++) 
             {
                     Node theAttribute = attributes2.item(a);
                     System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());

             }
          }

          NodeList n1 = e1.getElementsByTagName(KEY_COST);
        // System.out.println("title "+n.getLength());
          for (int k = 0; k < n1.getLength(); k++) {

              Element e2 = (Element) n1.item(k);
        //    System.out.println("node Url value");
             NamedNodeMap attributes2 = e2.getAttributes();
          //   System.out.println("attrlength"+attributes2.getLength());
             for (int a = 0; a < attributes2.getLength(); a++) 
             {
                     Node theAttribute = attributes2.item(a);
                     System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());

             }}

             NodeList n2 = e1.getElementsByTagName(KEY_DESC);
            // System.out.println("title "+n.getLength());
              for (int k = 0; k < n2.getLength(); k++) {

                  Element e2 = (Element) n2.item(k);
            //    System.out.println("node snippet value"+e2.getNodeName());
                 NamedNodeMap attributes2 = e2.getAttributes();
              //   System.out.println("attrlength"+attributes2.getLength());
                 for (int a = 0; a < attributes2.getLength(); a++) 
                 {
                         Node theAttribute = attributes2.item(a);
                         System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());

                 }
              }  

     }

//    menuItems.add(map);
}