
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
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

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:
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.