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



Home Answers Viewqa XML how to read this xml file
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


rupesh paunikar
how to read this xml file
1 Answer(s)      4 years and 4 months ago
Posted in : XML


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.

Advertisement
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 file = new File(data
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
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
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 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 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
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 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
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
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
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 convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
how to convert .xml file to csv file
how to convert .xml file to csv file  how to convert .xml file to csv file
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 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.... Example This example demonstrates that how to read a file in java. To read a file
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 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 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... java.io.*; /** * How To Read File In Java with BufferedReader class
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 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 properties file in java
How to read properties file in java Description of Example:- In this example... read properties file;- ResourceBundle. Properties Class.  ... with the assigned key in the properties data file. How we will create .properties
Advertisements
 

 

 

DMCA.com