Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa XML parser xml one page to another
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


parser xml one page to another


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


Related Tutorials/Questions & Answers:
parser xml one page to another
parser xml one page to another  parser xml one page to another
parser - XML
parser  how to convert XML file to flat file(text file) using DOM and SAX parser
Advertisements
Xml Parser
Xml Parser  Hi... please tell me about What parser would you use for searching a huge XML file? Thanks
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
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
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
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
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>
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
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
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.
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>
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
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
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
Parsing repeatitive xml elements using SAX Parser - XML
Parsing repeatitive xml elements using SAX Parser  Hi, I'm using SAX Parser to read an XML file. My XML file contains repeatitive tags. I'm able to retrieve all the values from the xml file. However, for repeatitive tags
How to parse the attributes values from XML using KXML parser in j2me
How to parse the attributes values from XML using KXML parser in j2me  hai team, i did xml parsing from serverside...i got a resultant value from xml,but here tag value name only came from parsing attribute name cant come
one frame update another frame
one frame update another frame  How do I make a link or form in one frame update another frame
How to parse the attributes from XML using KXML parser in j2me
How to parse the attributes from XML using KXML parser in j2me  Dear Sir, I have doubt in How to parse the attributes from XML using KXML parser in j2me ..I parse the dge name value..but i didn't do parsing the attributes
parsing word xml file using SAX parser - XML
parsing word xml file using SAX parser  i am parsing word 2003's XML file using SAX.here my question is,i want to write some tag elements which... this xml file and write that xml part into file? can somebody
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
force a page to go to another page using JavaScript
force a page to go to another page using JavaScript  How to force a page to go to another page using JavaScript
Reading xml file using dom parser in java with out using getelementby tag name
Reading xml file using dom parser in java with out using getelementby tag name  Hi, How to read the xml file using java with dom parser, but without...-Beginners/8384-java-with-xml-parsing.html but it is not reading attribute
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
SAX Parser
; The Simple API for XML (SAX) is a serial access parser API for XML. It is used... executes, the SAX parser recognizes and responds to each XML structure taking... the parser encounters a new XML tag or encounters an error, or wants to tell
Parsing The XML File Using JDOM Parser in JSP
Parsing The XML File Using JDOM Parser in JSP... with JDOM parser to parse the xml document. JDOM can read existing XML documents... native parser. XML parsing produces an xml parse tree from an XML document. Using
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
Two Pagination in one page
Two Pagination in one page  hai friends any one help me. how do u make two pagination script in same page i'm used some ajax coding one pagination script is working but another pagination is not working please help me urgent
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
create a one web page
create a one web page  create a one web page with stored values in a database
Advertisements
 

 

 

DMCA.com