Home Answers Viewqa XML Read XML in java

 
 


prashant
Read XML in java
1 Answer(s)      2 years and 10 months ago
Posted in : XML

Hi Deepak, I want to read a xml file which have only one element with multiple attributes with same tag name.
here is my file
<employee>
<firstname>Tom</firstname>
<lastname>Cruise</lastname>
<firstname>joil</firstname>
<lastname>Enderson</lastname>
<firstname>George</firstname>
<lastname>Bush</lastname>
<firstname>prashant</firstname>
<lastname>nikumbh</lastname>
</employee>
i tried to solve the problem but failed.the program which i hav developed shows only first record ie. only Tom Cruise.
please give me a solution.my java code is given below.
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

class XMLReader {

public static void main(String argv[]) {

try {
File file = new File("MyXMLFile.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("employee");
System.out.println("Information of all employees");

for (int s = 0; s < nodeLst.getLength(); s++)
{
Node fstNode = nodeLst.item(s);
Node lstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE)
{
Element fstElmnt = (Element) fstNode;
Element lstElmnt = (Element) lstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
for(int i=0;i< fstNmElmntLst.getLength();i++)
{
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println("First Name : " + ((Node) fstNm.item(0)).getNodeValue());
}

NodeList lstNmElmntLst = lstElmnt.getElementsByTagName("lastname");
for(int j=0;j< lstNmElmntLst.getLength();j++)
{
Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
NodeList lstNm = lstNmElmnt.getChildNodes();
System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
}



}

}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Thanks,
Prashant.
View Answers

July 21, 2010 at 10:46 AM


Hi Friend,

Try the following code:

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class DOMParserExample{
public final static String getElementValue( Node elem ) {
Node node;
if( elem != null){
if (elem.hasChildNodes()){
for( node = elem.getFirstChild(); node != null; node = node.getNextSibling()){
if( node.getNodeType() == Node.TEXT_NODE){
return node.getNodeValue();
}
}
}
}
return "";
}
public static void retrieveValue(Node node,int index) {
String nodeName = node.getNodeName();
String nodeValue=getElementValue(node);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 1; i++) {
buffer.append(" ");
}
System.out.println(nodeName + " = " + nodeValue);
NodeList children = node.getChildNodes();
for (int i = 1; i < children.getLength(); i++) {
Node child = children.item(i);

if (child.getNodeType() == Node.ELEMENT_NODE) {
retrieveValue(child,index + 2);
}
}
}
public static void main(String[] args) throws Exception {
File f=new File("C:/employee.xml");
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(f);
Node root = doc.getDocumentElement();
retrieveValue(root,0);
}
}

Thanks









Related Pages:
read xml
read xml   hi all, i want to ask about how to read an xml in java ME.. here is the xml file <data> <value> <struct> <member> <name> User_Name
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have only one element with multiple attributes with same tag name. here is my file... a solution.my java code is given below. import java.io.File; import
read xml elements
read xml elements  i want read xml data using sax parser in java. but is there any classes or methods to read xml elements
read xml elements
read xml elements  i want read xml data using sax parser in java. but is there any classes or methods to read xml elements
Read XML using Java
of all i need to read xml using java . i did good research in google and came to know...Read XML using Java  Hi All, Good Morning, I have been working..., XML Beans Jar and so on... So i request you all to suggest best way to read XML
how to read this xml file - XML
how to read this xml file  i want to read this xml file using java... read i have tried lot more , but i am not able to read this xml file... name=client menu=client action=read user employee add
read xml using java
read xml using java  <p>to read multiple attributes and elements from xml in an order.... ex :component name="csl"\layerinterfacefile="poo.c...;   Please visit the following link: Read XML data
Read the value from XML in java
Read the value from XML in java  Hi, i have an XML with the following code. I need to get the path("D... the permissions on that file.So how can i read that value. This is little urgent
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java
How to read value from xml using java?
How to read value from xml using java?  Hi All, I want to read value from following xml using java.. In <Line>,data is in format of key and value pair.. i want to read only values..could u plz help me in this?Thanks
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
read XML file and display it using java servlets
read XML file and display it using java servlets  sir, i can't access Xml which is present in my d drive plz can u should go through my code n tell me the things where i went wrong java servlet program protected void
Java + XML - XML
Java + XML  1) I have some XML files, read one xml... java...the attribute value..not sure now how to read the xml file passing..." pointing to a hello xml file ..read that file and get the value of the parent
XML- read Text Mode - Java Error in Windows - reg. - Java Beginners
XML- read Text Mode - Java Error in Windows - reg.  Dear All I'm creating the code as read the XML file in BufferedReader method. Here I pasted the code below:. (*) First read the XML file using FileInputStream
Data read. - XML
Data read.  How to store or read data in XML from Struts.Plz give me example. Thanx
java - XML
This is my XML file then how can read this XML file Using DOM & SAX parsers in java? How can write the same data into XML file using DOM parser? Could you
java - XML
java  How to read the values of XMLStreamConstants.CDATA in simple java
JDOM example in java, How to read a xml file in java.
JDOM example in java, How to read a xml file in java. In this tutorial, we will see how to  read a xml file in java. Code. Student.xml <?xml version="1.0"?> <Student >
JAVA - XML
JAVA  hi.. i want to talk to any SWT expert in JAVA... how can i do it?   Hi friend, For read more information,Examples and Tutorials on SWT visit to : http://www.roseindia.net/tutorials/swt/ Thanks
xml to html via java
xml to html via java  how to read xml into html using java code
Create XML - XML
Create XML   Hi, Can you please provide java code... is predefined from java file? Thanks in advance Sumanta  Hi friend... elements in your XML file: "); String str = buff.readLine(); int
XML
XML  create flat file with 20 records. Read the records using xml parser and show required details
Java get XML File
Java get XML File       In this section, you will study how the java application read the XML file. For this, you need to create a XML file. Here is the employee.xml
creating index for xml files - XML
creating index for xml files  I would like to create an index file for xml files which exist in some directory. Say, my xml file is like below: smith 23 USA john 25 USA ... ... All xml files in the directory have
How read xml file value in dojox chart - XML
How read xml file value in dojox chart  How to read the xml file value in dojox chart
XML - XML
. Please read xml Introduction at http://www.roseindia.net/xml/xml-introduction.shtml Read more tutorials on xml at http://www.roseindia.net/xml/ Thanks...XML  What is specific definatio of XML? Tell us about marits
how to read and write xml files in javascript
how to read and write xml files in javascript  hi m very new to javascript and xml,, now i want to read and write xml files in javascript,, please give me a sample code to do
XML Books
; Processing XML with Java Welcome to Processing XML with Java, a complete tutorial about writing Java programs that read... integrating XML with Java (and vice versa) you can buy. It contains over 1000 pages
xml
xml  how to creatte html file and validate using java and finally i need get web.xml file
XML - XML
XML XSD validation in java  Can anyone help in writing validation for XML XSD in Java
j2me with xml - Java Beginners
j2me with xml   shahzad.aziz1@gmail.com i am working in j2me and i want to read and display data using xml file. In j2me application KXML PARSER is use to read and display data. When i run my application in ?Wireless
Uses of XML
. Later on you can use programming language such a Java or PHP to read the xml...Uses of XML In this section we are discussing the importance of XML and see the uses of XML. The full form of XML is Extensible Markup Language. XML
how to write java data - XML
how to write java data  how to write data to xml file  Hi friend, Read for more information, http://www.roseindia.net/xml/dom/ Thanks
xml - XML
xml  how to match to xml file?can you give example with java code
XML Tutorial
kinds of structured information .     The Java /XML Tutorial: the Java XML Tutorial, is an online manual that can quickly get you... the Java API for XML Processing ((JAXP). To focus on XML with a minimum
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
xml - XML
xml  hi convert xml file to xml string in java ,after getting the xml string result and how to load xml string result? this my problem pls help..."); FileOutputStream output = new FileOutputStream("new.xml"); ReadXML xml = new
Reading an XML document using JDOM
Reading an XML document using JDOM       This Example shows you how to Read an XML document..., it is a tree based Java api. JDOM represents an XML document as a tree composed
XML- SAX Parser using JAXP API
In the previous issue of Java Jazz Up you have read about XML technology. In this issue, you will learn how XML technology works with Java using different kinds of XML..., the XML document is easily visible.     Read more information at: http
xml displaying a drives data.....
of server directory and generates the XML file. You can read this xml file in java...xml displaying a drives data.....  Hi all, I need a solution for displaying content of a drive(Ex: c , d , e ) in the browser using the XML
java and xml - XML
java and xml  Hi Deepak, I want learn xml and java(applications). Which editor is best usefull(trial--version) in my applications...; Hi friend, http://www.roseindia.net/xml/dom/ Thanks
xml file reading using java
xml file reading using java  hi deepak I want to read some data from xml file and send that output to particular email address using java   import org.w3c.dom.*; import org.w3c.dom.Node; import javax.xml.parsers.
read a file
read a file  read a file byte by byte   import java.io.File; import java.io.FileInputStream; public class ReadFileByteArray { public... go through the following link: http://www.roseindia.net/tutorial/java/core
Java-XML-DOM - XML
Java-XML-DOM  Hi! I need some help. I have to make java program that loads an xml file and from it builds DOM(later i will have to work with it - like using xpath in java find some value and replace it...). Since i'm new to java

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.