Home Answers Viewqa XML how to read this xml file

 
 


rupesh paunikar
how to read this xml file
1 Answer(s)      2 years and 11 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.
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 Pages:
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
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 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 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
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
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
Read the value from XML in java
Read the value from XML in java  Hi, i have an XML...:/Lakki/PermissionCheck/logs/RevAppserver.log") of log file in order to check the permissions on that file.So how can i read that value. This is little urgent
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 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 in xml? - XML
how to write in xml?  can anybody give the code how to write in xml...(new InputStreamReader(System.in)); System.out.print("Enter XML file name... Source source = new DOMSource(doc); // Create File to view your xml
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
Data read. - XML
Data read.  How to store or read data in XML from Struts.Plz give me example. Thanx
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...; <Actor> Rajani Kanth <Actress> Tamanna How do i get all
Read XML using Java
Read XML using Java  Hi All, Good Morning, I have been working... of all i need to read xml using java . i did good research in google and came to know..., XML Beans Jar and so on... So i request you all to suggest best way to read XML
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
Creating XMl file - XML
Creating XMl file   I went on this page: http://www.roseindia.net/xml/dom/createblankdomdocument.shtml and it shows me how to create an XML file, however there is something I don't understand. I have to create an XML file
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
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
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
Reading a xml file - JSP-Servlet
Reading a xml file  how to read a xml file using jsp and then i have to retrive a data from that file use it in code?  Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/parsing-xml.shtml
XML
XML  create flat file with 20 records. Read the records using xml parser and show required details
java read file
java read file  Hello i need some help... i want to read an MS Excel file in java so how to read that file
Read file in java
Read file in java  Hi, How to Read file in java? Thanks   Hi, Read complete tutorial with example at Read file in java. Thanks
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
Read and write file
Read and write file  HI, How to read and write file from Java program? Thanks   Hi, See the following tutorials: Java Write To File Read File 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
Java + XML - 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...Java + XML  1) I have some XML files, read one xml
xml
xml  how can i remove white space and next line when i copy stream to xml file
Read external file in PHP
Read external file in PHP  How to read external files in PHP in particular time duration and save that file with the latest date. How is it possible? Please explain with an example. Thanks
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
Xml and RecordStore
Xml and RecordStore  How to read xml file and Stored its data in RecordStore in j2me
Sorting Xml file - XML
Sorting Xml file  I have an xml file like this: Smith USA... sort my xml file. Here is my xslt..., my ouput file will be like this: Amy AUC Bob USA John UK
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
Java read binary file
Java read binary file  I want Java read binary file example code that is easy to read and learn. Thanks   Hi, Please see the code... more examples at Java File - Learn how to handle files in Java with Examples
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
Read Video File
Read Video File  how to read a video file, after that i want to encrypt and decrypt it. please help me and if u can send me some hint or source code on swarit.it@gmail.com Thanks & Regards Swarit Agarwal
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
how to convert .xml file to csv file
how to convert .xml file to csv file  how to convert .xml file to csv file
xml
xml  how to creatte html file and validate using java and finally i need get web.xml file
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
XML to JSP
XML to JSP  How to access the following XML tag Title tag.../parsing-xml.shtml The above link will provide you an example that will read an xml file and display the data into jsp file
XML - XML
and demarits of XML? use of XML? How we can use of XML?  Hi, 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
Java read file in memory
Java read file in memory In this section, you will learn how to read a file... and then read the file... for the  file, and then call map( ) to produce a MappedByteBuffer, which
read restricted pdf file
read restricted pdf file  i have restricted file. package Encryption; import java.io.*; import java.util.*; import com.lowagie.text.*; import... is it possible or not ?how

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.