can't read my xml file in java

can't read my xml file in java

i've a xml file like this :

 <table ID="customer">
<T>
<C_CUSTKEY>1</C_CUSTKEY>
<C_NAME>Customer#000000001</C_NAME>
<C_ADDRESS>IVhzIApeRb ot,c,E</C_ADDRESS>
<C_NATIONKEY>15</C_NATIONKEY>
<C_PHONE>25-989-741-2988</C_PHONE>
<C_ACCTBAL>711.56</C_ACCTBAL>
<C_MKTSEGMENT>BUILDING</C_MKTSEGMENT>
<C_COMMENT>regular, regular platelets are fluffily according to the even attainments. blithely iron</C_COMMENT>
<abc>abc</abc>
</T>

<T>
<C_CUSTKEY>2</C_CUSTKEY>
<C_NAME>Customer#000000001</C_NAME>
<C_ADDRESS>IVhzIApeRb ot,c,E</C_ADDRESS>
<C_NATIONKEY>15</C_NATIONKEY>
*// nothing <C_PHONE>...</C_PHONE>*
<C_ACCTBAL>711.56</C_ACCTBAL>
<C_MKTSEGMENT>BUILDING</C_MKTSEGMENT>
<C_COMMENT>regular, regular platelets are fluffily according to the even attainments. blithely iron</C_COMMENT>

//nothing ... in this row

and my some java codes :

 private String getResults(TopDocs docs) {
        StringBuilder htmlFormat = new StringBuilder();
        htmlFormat.append("<html><body>");
        htmlFormat.append("ditemukkan sebanyak : <b>" + docs.totalHits +"</b> dokumen <br>");
  for (ScoreDoc sd : docs.scoreDocs) {
            int docId = sd.doc;
            try {
                org.apache.lucene.document.Document doc = searcher.doc(docId);
                htmlFormat.append("<hr>" + "score: ").append(sd.score).append("<br>");
                htmlFormat.append("<b>No.Kustomer</b>: ").append(doc.get("C_CUSTKEY")).append("<br>");
                htmlFormat.append("<b>Nama</b>: ").append(doc.get("C_NAME")).append("<br>");
                htmlFormat.append("<b>Alamat</b>: ").append(doc.get("C_ADDRESS")).append("<br>");
                htmlFormat.append("<b>Kewarganegaraan</b>: ").append(doc.get("C_NATIONKEY")).append("<br>");
                htmlFormat.append("<b>Telepon</b>: ").append(doc.get("C_PHONE")).append("<br>");
                htmlFormat.append("<b>Sisa Saldo</b>: ").append(doc.get("C_ACCTBAL")).append("<br>");
                htmlFormat.append("<b>Segmen</b>: ").append(doc.get("C_MKTSEGMENT")).append("<br>");
                htmlFormat.append("<b>Komentar</b>: ").append(doc.get("C_COMMENT")).append("<br>");
                htmlFormat.append("<b>Tes</b>: ").append(doc.get("abc")).append("<br>");
                } catch (IOException ex) {
                 }
        }
        htmlFormat.append("</body></html>");
        return htmlFormat.toString();
    }

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 the xml file?thx for ur advice

View Answers

January 23, 2012 at 3:41 PM

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

You can visit the following link in order to check another example:

http://www.roseindia.net/xml/dom/GetData.shtml









Related Tutorials/Questions & Answers:
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..._COMMENT> //nothing ... in this row and my some java codes
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..._COMMENT> //nothing ... in this row and my some java codes
Advertisements
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..._COMMENT> //nothing ... in this row and my some java codes
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..._COMMENT> //nothing ... in this row and my some java codes
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..._COMMENT> //nothing ... in this row and my some java codes
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..._COMMENT> //nothing ... in this row and my some java codes
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
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
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 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
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 
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... for automating some of the functionaries. And here goes my requirements. Read huge complex
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
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
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
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
Java read binary file
Java read binary file  I want Java read binary file example code... at Reading binary file into byte array in Java. Thanks   Hi, There is many more examples at Java File - Learn how to handle files in Java with Examples
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
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
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
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
xml file creation in java
xml file creation in java  how to create xml file in java so...; Please visit the following links: http://www.roseindia.net/tutorial/java/xml...;Please visit the following links: http://www.roseindia.net/tutorial/java/xml
Java read lines from file
java file line by line. My file is very big around 6.5GB so it's not possible to read the whole file in one go. So, in my case reading file line by line is only...Java read lines from file  Any code example related to Java read
read complete file as string java
read complete file as string java  Hi, How I can read read complete file as string java? Thanks   Hi, You can do like this in one line: String fileContent = new String(Files.readAllBytes(Paths.get(fileName
Java file read write operation
Java file read write operation  how to read and write the data from text file.Suppose i have text file with 4 fields name ,roll no ,marks1,marks2..."); oos.writeInt(8000); oos.writeObject("Ankit"); oos.writeObject("Java
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... is the program to read a file in Java. import java.io.*; public class FileRead
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 - read file line by line in Java
in Java? Why I can't read a big text file in memory? Thanks   A text file in Java can be read line by line with the help of BufferedReader class...java - read file line by line in Java  Hi, how one can read a text
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
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
Java read file
There are many ways to read a file in Java. DataInputStream class is used to read text File line by line. BufferedReader is also used to read a file in Java...(in)); readline() is used to read the next line. Example of Java Read File:ADS_TO_REPLACE_3
file read
file read  hi i am reaing from a file which has punjabi words. can some one help with me some code
Sorting Xml file - XML
sort my xml file. Here is my xslt.... Thanks.  I am trying to do it with xslt. But it can't sort my xml...Sorting Xml file  I have an xml file like this: Smith USA
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
parsing XML file to get java object - XML
parsing XML file to get java object  Hello, I'm facing a problem in parsing XML file to get the java object. I've tried to retrieve data from XML file using SAX parser. my XML file structure is the following
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 Java provides IO package to perform reading and writing operations with a file. In this section you will learn how to read a text... to read file in Java?" Another way of reading a file: The another
read excel file from Java - Java Beginners
read excel file from Java  How we read excel file data with the help of java?  Hi friend, For read more information on Java POI visit to : http://www.roseindia.net/java/poi/ Thanks
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, binary data etc. To read a file in Java we can use following of the classes
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
What is the fastest way to read a file in Java?
What is the fastest way to read a file in Java?  Hi, I am reading my file using the Scanner class. My code is below: import java.io.*; import...); } Read the details at How to read file line by line in Java?   
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.
Java read file contents into byte array
Java read file contents into byte array  Hello share the code of java read file contents into byte array with me. It's urgent. Thanks   Hi... at : Java example for Reading file into byte array Thanks
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
Java read properties file
Java read properties file In this section, you will learn how to read... Video tutorial of reading properties file in Java: "How to read properties file in Java?" Here is the code: import java.io.*; import
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
Java read entire file into byte array
Java read entire file into byte array  Where is the example of Java read entire file into byte array on your website? Thanks   Hi, Its simple you can use insputStream.read(bytes); method of InputStream class. Read
Java Read CSV file
Java Read CSV file In this tutorial, you will learn how to read csv file. CSV (Comma Separated Value) is a common type of data file which can be exported... opened a file data.csv  and created a BufferedReader object to read

Ads