Parsing a local xml file

Put your problem here if it does not fit any of the other categories.

Parsing a local xml file

Postby jrambharose » Fri Mar 18, 2011 9:29 pm

I transfered a xml to the AVD and now i'm trying to get at the data. It seems to be crashing where i try to get the first elements tag name.

My XML format
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <DataSet name = "Client Data">
  2.         <Client name = "Client1">              
  3.                 <Matter>0001</Matter>
  4.                 <Matter>0002</Matter>
  5.                 <Matter>0003</Matter>
  6.                 <Matter>0004</Matter>
  7.                
  8.                 <Purpose>Taxi</Purpose>
  9.                 <Purpose>Clubbing</Purpose>
  10.                 <Purpose>Strippers</Purpose>
  11.                 <Purpose>Refreshments</Purpose>
  12.                 <Purpose>Petty Cash</Purpose>
  13.         </Client>
  14.  
  15.         <Client name = "Msoft">        
  16.                 <Matter>9001</Matter>
  17.                 <Matter>8002</Matter>
  18.                 <Matter>7003</Matter>
  19.                 <Matter>6004</Matter>
  20.  
  21.                 <Purpose>Exclusive payoff</Purpose>
  22.                 <Purpose>Clubbing</Purpose>
  23.                 <Purpose>Something else</Purpose>
  24.                 <Purpose>Refreshments</Purpose>
  25.                 <Purpose>Petty Cash</Purpose>
  26.         </Client>
  27. </DataSet>
  28.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


My parsing code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         try
  2.         {
  3.                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  4.         DocumentBuilder db = dbf.newDocumentBuilder();
  5.                        
  6.         File table = new File("/data/config.xml");
  7.         Document doc = db.parse(table);
  8.         doc.getDocumentElement().normalize();
  9.                        
  10.         NodeList dataList = doc.getElementsByTagName("Client");
  11.                        
  12.         for(int i =0; i < dataList.getLength(); i++)
  13.         {
  14.                 m_cdData.add(new ClientData());
  15.                                
  16.                 Node node = dataList.item(i);
  17.                 Element elmnt = (Element)node;
  18.                 m_cdData.get(i).setClient(elmnt.getNodeValue().toString());
  19.                                
  20.                 NodeList matterList = elmnt.getElementsByTagName("Matter");
  21.                 for(int j=0; j < matterList.getLength(); j++)
  22.                 {
  23.                         Element matterElmnt = (Element)matterList.item(j);
  24.                         m_cdData.get(i).setMatter(j, matterElmnt.getNodeValue().toString());
  25.                 }
  26.                                
  27.                 NodeList purposeList = elmnt.getElementsByTagName("Purpose");
  28.                 for(int k=0; k < purposeList.getLength(); k++)
  29.                 {
  30.                         Element purposeElmnt = (Element)purposeList.item(k);
  31.                         m_cdData.get(i).setPurpose(k, purposeElmnt.getNodeValue().toString());
  32.                 }
  33.         }
  34. }
  35. catch (ParserConfigurationException e)
  36. {
  37.         // TODO Auto-generated catch block
  38.         e.printStackTrace();
  39. }
  40. catch (SAXException e)
  41. {
  42.         // TODO Auto-generated catch block
  43.         e.printStackTrace();
  44. }
  45. catch (IOException e)
  46. {
  47.         // TODO Auto-generated catch block
  48.         e.printStackTrace();
  49. }  
  50. }
  51.  
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


I know it finds the list of all the elements in dataList, value is null where i try to pull the client name. Im guessing my formatting is off.
jrambharose
Freshman
Freshman
 
Posts: 7
Joined: Wed Mar 02, 2011 6:57 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests