how to read this xml file

how to read this xml file

i want to read this xml file using java(using struts2 or using jsp)
and i want result as

name=admin
menu=user employee
action=add, delete, modify
name=normal
menu=employee
action=add, modify
name=client
menu=client
action=read

<roles>
<role name="admin">
<menu>user</menu>
<menu>employee</menu>

<action>add</action>
<action>delete</action>
<action>modify</action>
</role>

<role name="normal">
<menu>employee</menu>

<action>add</action>
<action>modify</action>
</role>

<role name="client">
<menu>client</menu>

<action>read</action>
</role>
</roles>

i have tried lot more , but i am not able to read this xml file as i want. So, please help me for this, i will be very thankful to u.
Thanking u.
View Answers

June 18, 2010 at 5:58 PM

Hi Friend,

We are providing you java code:

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import static org.w3c.dom.Node.*;

public class MainClass {

public static void main(String args[])throws Exception {
File f=new File("C:/data.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDoc = builder.parse(f);
listNodes(xmlDoc.getDocumentElement(),"");
}
static void listNodes(Node node,String indent) {
String nodeName = node.getNodeName();
System.out.print(indent+" "+nodeName.replaceAll("#text","").replaceAll("roles","").replaceAll("role",""));
if(node.getNodeType() == TEXT_NODE){
System.out.print(((Text)node).getWholeText());
} else if(node.hasAttributes()) {
NamedNodeMap attrs = node.getAttributes();
for(int i = 0 ; i<attrs.getLength() ; i++) {
Attr attribute = (Attr)attrs.item(i);
System.out.print(attribute.getName()+" = "+attribute.getValue());
}
}
NodeList list = node.getChildNodes();
if(list.getLength() > 0) {
for(int i = 0 ; i<list.getLength() ; i++) {
listNodes(list.item(i),indent);
}
}
}
}

Hope that it will be helpful for oyu.
Thanks









Related Tutorials/Questions & Answers:
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
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
Advertisements
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
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...;File(data);       DOMBuilder builder 
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... ServletException, IOException { response.setContentType("text/xml"); FileRead fr
How to create XML file - XML
How to create XML file  Creating a XML file, need an example. Thanks!!  To create XML file you need plain text editor like note pad. After creating your file save it with .xml extension.  Hi,Java programming
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 prepare XML file?
How to prepare XML file?  Hi, I want to prepare XML File, can you tell me how should I do it? Thanks.   Hello, Following links can help you create an XML file.. Create XML File using Servlet How to generate
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
How to Read a File in Java
How to Read a File in Java? In this section we are going to know, How to read a file in Java. We have to follow three step to read a File. First get... to read the Content of file . Read A File: Reading a file is nothing
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>... of the first value node after the data node, i just can get only 1 struct but not 2, how
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
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
how to convert .xml file to csv file
how to convert .xml file to csv file  how to convert .xml file to csv file
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
how to seach a keyword in a xml file - XML
how to seach a keyword in a xml file  i have a xml file. i wanted to read and store all the tag values. i have a html form in which when i enter some keyword say "a" then it has to display the tag values starting with a letter
How to write to xml file in Java?
How to write to xml file in Java?  Hi Friends, Can anyone help me how to write in xml file in Java programming. Please fill free to give example or reference website for getting example.   Hi, To write in xml file
How To Read File In Java
How To Read File In Java In this section we will discuss about about how data of a file can be read in Java. A file can contain data as bytes, characters..._TO_REPLACE_1 Example This example demonstrates that how to read a file in java
How to read file in java
How to read file in java Java provides IO package to perform reading and writing operations with a file. In this section you will learn how to read a text... the given file name. read()-The read() method of FileInputStream class reads
how to modify content of XML file
how to modify content of XML file  hi, I want to modify content of a xml file.There are two tags of similar name say <ContentName>C:\Myinfo... to xml file to replace "XYZ".Every time with the content of text box the file should
How to create one xml file from existing xml file's body?
How to create one xml file from existing xml file's body?  Hi, i'm working with content optimization system.I want to know how we can take all data from an xml doc's body to develope another xml with that content.I'm using JDOm
How To Read File In Java with BufferedReader
How To Read File In Java with BufferedReader class - example code This tutorial shows you how you can read file using BufferedReader class in your program... class: import java.io.*; /** * How To Read File In Java with BufferedReader
How to read text file in Servlets
How to read text file in Servlets       This section illustrates you how to read text file in servlets. In this example we will use the input stream to read the text
How to make a file read-only
Description: This example demonstrate how to create a file and how to revoke... a file with specified name with read-only attribute. You can also check the the file properties, by default it is read-only. ADS_TO_REPLACE_1
How to read properties file in java
How to read properties file in java Description of Example:- In this example... is the video tutorial of: "How to read properties file in Java?" How we... read properties file;-ADS_TO_REPLACE_1 ResourceBundle. Properties Class
How to read an eml file? - Java Beginners
How to read an eml file?  Hello dears, now i want to read en eml file stored in a folder. is it possible to use the same code for reading a txt file? or do i have to use javamail api? Thanks Spalax
how to read the .proprties file from struts - Struts
how to read the .proprties file from struts   errpr is :file not found exception:applicationresource.proprties file {system canot find file path"; How to set the file path.  Hi Friend, It seems that you haven't
how to read file using InputStreamReader in java
how to read file using InputStreamReader in java  Hi, I want to learn to use the InputStreamReader class of Java and trying to read a text file with the class. how to read file using InputStreamReader in java? Thanks  
how to read a text file with scanner in java
how to read a text file with scanner in java  Hi, I am looking... class. how to read a text file with scanner in java? Thanks   Hi... void main(String[] args) throws FileNotFoundException { // File to read
How to read the .doc/ .docx file in Java Program
How to read the .doc/ .docx file in Java Program  Hi, I am beginner in Java programming language. Can anybody explain How to read .doc file in Java... to read the word document file. For doing this we will make class HWPFDocument which
How to read excel2007 file - JSP-Servlet
How to read excel2007 file  Dear sir , I am able to read a excel 97-2003(.xls)files using apache poi.But for 2007(.xlsx)files to read... Friend, For reading .xls file you used HSSFWorkbook. but for .xlsx file reading
How to Read a file line by line using BufferedReader?
How to Read a file line by line using BufferedReader?  Hello Java... problem is to find the best way to read the file in Java. I just searched the google... to Read a file line by line using BufferedReader, efficiently and using less memory
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...[]) { try { File file = new File("MyXMLFile.xml
How to read a line from a doc file
How to read a line from a doc file  How to read a line from a doc file   Hi Friend, To run the following code, you need to download POI... { public static void main(String[] args) { File file = null; WordExtractor
how to read text file in jtable in netbeans7.0
how to read text file in jtable in netbeans7.0  text file... want to displaythe above .txt file in jtable as following format having 3... area which is design below table in netbeans IDE. how to do that ? pls help me
how to convert text file to xml file in java. - XML
how to convert text file to xml file in java.  Hi all, I m having some problem. Problem is I want to convert a text file which is having the no of record(i.e no of different line of information)to a xml file through java
read a file
read a file  read a file byte by byte   import java.io.File..._TO_REPLACE_1 public static void main(String[] args) { File file = new File("D://Try.txt"); try { FileInputStream fin = new FileInputStream(file
How to read properties file in Java?
a property file in Java: "How to read properties file in Java?" ADS... to value. The java.util.Properties class is used to read the properties file...Example program of reading a properties file and then printing the data
file read
file read  hi i am reaing from a file which has punjabi words. can some one help with me some code
simple code to write an read and write the login detail to a xml file using javascript ( username and password )
simple code to write an read and write the login detail to a xml file using... to write and read the login details (username and password )into a xml file using javascript. (XML database
How to read the contents in a file which is of input type file
How to read the contents in a file which is of input type file  I have a jsp page wherein i have to passs the file as input and read the contentsn... //In this servlet page i have tried to read the contents of a file in doPost method. I have used
Data read. - XML
Data read.  How to store or read data in XML from Struts.Plz give me example. Thanx
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

Ads