parser xml one page to another

Ads

 
 

Share on Google+Share on Google+

Java Coder
parser xml one page to another
1 Answer(s)      5 years and 8 months ago
Posted in : XML

parser xml one page to another

Ads
View Answers

February 29, 2012 at 3:22 PM


1)file.xml:

<school>
<student>
<name>Angelina</name>
<address>Delhi</address>
</student>
<student>
<name>Martina</name>
<address>Mumbai</address>
</student>
</school>

2)Student.java:

class Student 
{
    String name;
    String address;

    Student(String name,String address){
        this.name=name;
        this.address=address;
    }
    public String getName(){
        return name;
    }
    public String getAddress(){
        return address;
    }
}

3)ParseXmlFile.java:

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

Ads









Related Tutorials/Questions & Answers:
parser xml one page to another
parser xml one page to another  parser xml one page to another
calling one jsp from another jsp page
calling one jsp from another jsp page  need coding for calling one jsp from another jsp including the xml file.Its urgent
Advertisements
to bring checked data from one page to another
to bring checked data from one page to another  thanks for your help... there is one jsp page , on it there is a link, on click of which it is opening second jsp page which is connected with servlet at the back and through which i am
calling one jap page from another jsp page
calling one jap page from another jsp page  i created a button in one jsp page i need to call another jsp page as an action to that button. so how can i call.. plz any one explain. its urgent
Xml Parser
Xml Parser  Hi... please tell me about What parser would you use for searching a huge XML file? Thanks
parser - XML
parser  how to convert XML file to flat file(text file) using DOM and SAX parser
JavaScript XML Parser
JavaScript XML Parser    ... to access and manipulate XML document using DOM parser in javascript. XML parser.... The method xmlDoc.load("data.xml") allows the parser to load an XML
sax parser for xml
sax parser for xml  sax parser code that works with any XML i.e independent of any XML to extract data from XML
passing textbox value from one page to another page using html n javascript
passing textbox value from one page to another page using html n javascript  on button onclick event pass text box value and i want to pass value... on the othe (next/prevoious)page please help me   1)page1.html: <html>
one frame update another frame
one frame update another frame  How do I make a link or form in one frame update another frame
Java XML parser
Java XML parser  Hi friends, i am new to java XML parsing. i need to parse the following xml response which i got from a url hit: <response> <status>SUCCESS</status> <app-config> <CHECK_LOW_SCORE>
program to enter values from one jsp page and then do calculation on some formula and that results is displayed in another jsp
program to enter values from one jsp page and then do calculation on some formula and that results is displayed in another jsp  <%@ page language="java" import="java.util.*;"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
add one row after another
add one row after another  how i add one row after another on click of add button $(document).ready(function() { var counter = 2; $("#addData").click(function() { if (counter > 10
SAX Parser for huge XML file
SAX Parser for huge XML file  Hi.... if the XML file is small...("Enter XML file name:"); String xmlFile = bf.readLine(); File file = new File(xmlFile); if (file.exists()){ System.out.print("Enter XML tag name
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... could understand was while calling the event handler of the SAX Parser i.e.
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
converting one file format to another
converting one file format to another  Hi ser I need a code to export data from data grid to PDF and XL format plz help me out
Copy one file into another
Copy one file into another In this section, you will learn how to copy content of one file into another file. We will perform this operation by using...;This string is copied from one file to another\n"); bf.close(); InputStream
Hibernate one to one mapping using xml
Hibernate one to one mapping using xml  Hi, How to create hibernate one to one mapping using xml? I have to create the mapping using the *.hbm.xml..., In case of one-to-one mapping for each reach in a table there is corresponding only
Hibernate One to One Mapping using XML
In this section, you will learn One to One mapping of table in Hibernate using Xml
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... that value append to send web service and got reult...here i got xml feed now i want
how to use string of one class into another Class
how to use string of one class into another Class  I created textField(JTextField t1=new JTextField();(it is in ClassA. The input of this textField i.e t1.getText() is to be used in different(another Class say in ClassB. How can
Convert One Unit to Another
Convert One Unit to Another       Again in this section, you will learn to convert One Unit to Another. The following program helps you in converting some more units to another
File copy from one directory to another in java
File copy from one directory to another in java  I am trying to find very good code example for copying a file from one directory to another...? Is there any size issue while copying the file from one directory to another
how to set the tables one is left and another one is right - JSP-Servlet
how to set the tables one is left and another one is right  Here two tables are combined.i need to set the one table is left another one is right and ineed to gap between the two tables Document document=new Document
update one table from another table
update one table from another table  hello, i need to update one table fields by using another table fields. I have to tables Products... enter the Quantity field in table 'Products_Purchased'In table 'Products' one item
Read Complex XML using DOM/SAX Parser.
Read Complex XML using DOM/SAX Parser.  I have a XML file which is having two types of Items. I want to extract all details of a type only. My XML goes something like this <Movie> <Bollywood> <Actor> Shah Rukh
how to change password into one jsp to another jsp
how to change password into one jsp to another jsp  i have two jsps in one jsp i give one password and confirm password, in another jsp i want change...; </html> 2)changePassword.jsp <%@page import="java.sql.*"%> <
Java - Copying one file to another
Java - Copying one file to another   ... how to copy contents from one file to another file. This topic is related... This program copies one file to another file. We will be declaring a function
passing data from one jframe to another
passing data from one jframe to another  I have two JFrame,built... in the first Jframe to another JLabel in the other JFrame.Can you please help me,how...){ String value=text.getText(); NextPage page=new NextPage(value
How to forward the control from one jsp to another?
How to forward the control from one jsp to another?  Hi! This is Prasad Jandrajupalli. I have the 3 JSP's, but I want communicate with each other.i.e. First JSP is communicating with the second JSP, Second JSP

Ads

 
Advertisement null

Ads