2012-10-04 96 views
-1
<taxmann> 
    <docdetails> 
    <info id="104010000000007617" date="19780225"> 
    <physicalpath>\\192.168.1.102\CMS\DATA</physicalpath> 
    <filepath isxml="N"> 
    \NOTIFICATIONS\DIRECTTAXLAWS\HTMLFILES\150025021978.htm 
    </filepath> 
    <summary></summary> 
    <description></description> 
    <heading> 
    2187 [S.O.1500] | Section 35(1)(ii) of the Income-tax Act, 1961 - Scientific research expenditure - Approved scientific research associations/institutions 
    </heading> 
    <correspondingcitation/> 
    <hasfile>YES</hasfile> 
    <sortby>20120328160152743</sortby> 
    <parentid></parentid> 
    <parentchapterid></parentchapterid> 
    </info> 
    </docdetails> 
</taxmann> 

代码:如何解析的XML项和分项解析

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


     } 

这是我的XML格式,我想XMLPARSE并希望dsiaply编号,日期,标题IM能够显示标题,但我不打印日期和身份证可以请你告诉我我将如何实施它。这是我的代码打印标题请修改我的代码和打印标题,ID,日期..

+0

请看到这一点: http://xstream.codehaus.org/tutorial.html – Shrikant

+0

可以添加我的代码逻辑请coz我试图做很多不能做到这一点,我已经做了这个例子,但无法理解 – user1693501

+0

你可以粘贴XML的URL? – mukesh

回答

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

它可以帮助您吗? – mukesh

+0

对不起,我无法解析你的XML,因为它给出了响应代码500,我认为它有一些restrication从服务器。 – mukesh

+0

但我已经解析了其他xml,满足您的要求。 – mukesh

1

1.Declaration

 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"; 

2.Parsing

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); 
} 
+0

试试吧,它解析的是其他的xml。 – mukesh