Xml parsing

Xml parsing

Hi..i tried the above code but I unable to know where can i place the xml file..?

View Answers

April 21, 2012 at 12:49 PM

import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class ParseXmlFile{

 public static void main(String argv[]){
 ArrayList<Student> list=new ArrayList<Student>();
  try{
  File file = new File("c:\\file.xml");
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document document = db.parse(file);
  document.getDocumentElement().normalize();
  System.out.println("Root element "+ document.getDocumentElement().getNodeName());
  NodeList node = document.getElementsByTagName("student");
  System.out.println("Information of the students");

  for(int i = 0; i < node.getLength(); i++){
  Node firstNode = node.item(i);
  if(firstNode.getNodeType()== Node.ELEMENT_NODE){
  Element element = (Element) firstNode;
  NodeList firstNameElemntList = element.getElementsByTagName("name");
  Element firstNameElement = (Element) firstNameElemntList.item(0);
  NodeList firstName = firstNameElement.getChildNodes();
  Node n1=firstName.item(0);
  String name=n1.getNodeValue();

  NodeList lastNameElementList = element.getElementsByTagName("address");
  Element lastNameElement = (Element)lastNameElementList.item(0);
  NodeList lastName = lastNameElement.getChildNodes();
  Node n2=lastName.item(0);
  String address=n2.getNodeValue();
  list.add(new Student(name,address));
   }
  }
  for(Student st:list){
  System.out.println(st.getName()+"\t"+st.getAddress());
  }
  }
  catch (Exception e) {
  e.printStackTrace();
  }
 }
}

Put xml file C drive

Ads









Related Tutorials/Questions & Answers:
Xml parsing
Xml parsing  Hi..i tried the above code but I unable to know where can i place the xml file
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
Advertisements
xml parsing
xml parsing  my task is when i wll give java file to a program that program interpret that file separate each and every keywords and indentifiers... xml.. please anyone of you give some idea
XML parsing to Mysql database
XML parsing to Mysql database  Can someone please post the code for parsing an XML file into Mysql database using SAX
parsing xml with jquery
parsing xml with jquery  Please explain how to parse a xml file into jquery. Thanks
parsing xml using sax
parsing xml using sax  how to get values if the root elements are same and their attributes are different
xml document parsing
xml document parsing   Hi Guys, Xml document has been inserted into the database,how do i parse the xml document stored in database,so that xml document can be retrieved on to JSP file using jdbc/odbc connection. Please help me
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
problems in parsing the xml with the special characters
problems in parsing the xml with the special characters  Hi, I have a problem, in while parsing the xml with special characters apstrophe('). I am... parse this string in xml path. i am not able to fetch the adam's string in the xml
XML Parsing from php web server in J2ME
XML Parsing from php web server in J2ME  Hello Team, You gave some tips for XML parsing in java, i need some additional tips for xml parsing from php web server in j2me... Thanks and Regards Alagu
XML Parsing Using Sax Parser in J2ME for serverside
XML Parsing Using Sax Parser in J2ME for serverside  Hai team, i have doubt in Parsing using sax parser in serverside plz help me for xml parsing in j2me using sax parser on server side thanks in advance... regards Selva
Problem facing in SAX Parsing - XML
Problem facing in SAX Parsing  I have facing the issue in SAX Parsing like i have got the xml and the xml structure is like below... can find the above xml there is no CountryCode in the second record and 4th
XML Parsing Using Sax Parser in J2ME
XML Parsing Using Sax Parser in J2ME  Hai team, I hope you, You should help me, I have trouble in Xml parsing... I have decoded value, what... to do xml parsing help me for that... Regards Alagu
complex xml parsing and storing in database - XML
complex xml parsing and storing in database  Hi Experts ,i want to parse my xml document and store it in mysql database. This is my code. How to parse this complex data. EDI_DC40 800 0000000000557748
Truncating issue while parsing XML with SAX Parser.
Truncating issue while parsing XML with SAX Parser.  I am Parsing one xml file and after parsing I have to enter the data into the database using hibernate. The problem is while parsing some elements, its not getting the complete
Question about parsing XML document with DOM in JAVA.
Question about parsing XML document with DOM in JAVA.  Hi, I need to parse a XML file witch has info about classes. So i have for each class to have... it with multiple for loops, one iside the other but it doesnt work. My XML is this below
How to implement xml parsing in iphone ? -RV
How to implement xml parsing in iphone ? -RV  In .h file @interface RootViewController : UITableViewController<NSXMLParserDelegate>{ IBOutlet UITableView *page1; NSMutableArray *titlearray
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.13.0.662 is released. Learn to use sonar-xml-parsing version 1.13.0.662 in Maven based Java projects
1.13.0.662 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.13.0.662. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.14.0.678 is released. Learn to use sonar-xml-parsing version 1.14.0.678 in Maven based Java projects
1.14.0.678 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.14.0.678. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.14.0.681 is released. Learn to use sonar-xml-parsing version 1.14.0.681 in Maven based Java projects
1.14.0.681 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.14.0.681. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.14.1.690 is released. Learn to use sonar-xml-parsing version 1.14.1.690 in Maven based Java projects
1.14.1.690 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.14.1.690. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.15.0.699 is released. Learn to use sonar-xml-parsing version 1.15.0.699 in Maven based Java projects
1.15.0.699 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.15.0.699. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.16.0.719 is released. Learn to use sonar-xml-parsing version 1.16.0.719 in Maven based Java projects
1.16.0.719 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.16.0.719. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.17.0.740 is released. Learn to use sonar-xml-parsing version 1.17.0.740 in Maven based Java projects
1.17.0.740 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.17.0.740. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.18.0.756 is released. Learn to use sonar-xml-parsing version 1.18.0.756 in Maven based Java projects
1.18.0.756 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.18.0.756. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.19.0.786 is released. Learn to use sonar-xml-parsing version 1.19.0.786 in Maven based Java projects
1.19.0.786 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.19.0.786. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.20.0.801 is released. Learn to use sonar-xml-parsing version 1.20.0.801 in Maven based Java projects
1.20.0.801 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.20.0.801. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.21.0.829 is released. Learn to use sonar-xml-parsing version 1.21.0.829 in Maven based Java projects
1.21.0.829 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.21.0.829. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.10.1.446 is released. Learn to use sonar-xml-parsing version 1.10.1.446 in Maven based Java projects
1.10.1.446 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.10.1.446. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.22.0.848 is released. Learn to use sonar-xml-parsing version 1.22.0.848 in Maven based Java projects
1.22.0.848 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.22.0.848. Developer can
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.23.0.957 is released. Learn to use sonar-xml-parsing version 1.23.0.957 in Maven based Java projects
1.23.0.957 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest...; org.sonarsource.analyzer-commons - sonar-xml-parsing library is 1.23.0.957. Developer can

Ads