Need an urgrent help
Here is my program: I want to generate a xml file from my data . but I could not do it .some one can help me on this problem please.
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
public class XML {
// private final static String CONTENT_FILE =
private final static String STRUCTURE_FILE = "D://structure1.txt";
private final static String OUTPUT_CXML_FILE = "D://dmoz_cxml.xml";
private final static String TEMP_HASH_FILE = "D://hash.tmp";
private final static int STARTING_CLASS_ID = 8;
private final static boolean PROCESS_STRUCTURE = true;
private final static boolean PROCESS_CONTENT = false;
private final static int NODES_INTERVAL_REPORT = 10000;
private final static int DOCS_INTERVAL_REPORT = 100000;
public static void main(String[] args) {
//first parse the structure file
int errs = 0;
String line;
File f = new File(OUTPUT_CXML_FILE);
FileWriter fr = null;
PrintWriter pr = null;
if (f.exists()) f.delete();
try {
f.createNewFile();
fr = new FileWriter(f);
pr = new PrintWriter(fr);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
pr.println("<?xml version = \"1.0\" encoding= \"UTF-8\"?>");
pr.println("<CtxContent ctxid=\"" + STARTING_CLASS_ID + "\" name=\"DMOZ_full\" description=\"null\" timestamp=\"2006-03-20 12:24:11.656\">");
try {
System.out.println("Processing nodes information...");
int currentNode = 0;
pr.println("<SetOfNodes>");
HashMap<String, String> pathToID = new HashMap<String, String>();
HashMap<String, String> nodesToParents = new HashMap<String, String>();
BufferedReader br = new BufferedReader(new FileReader(STRUCTURE_FILE));
while ((line = br.readLine()) != null) {
currentNode++;
if ((currentNode % NODES_INTERVAL_REPORT) == 0) System.out.println("Processed: " + currentNode + " nodes");
StringTokenizer st = new StringTokenizer(line, "/");
String nodeID = st.nextToken().trim();
String category = st.nextToken().trim();
String path = st.nextToken().trim();
if (pathToID.put(path, nodeID) != null) System.out.println("DUPLICATE NODE!");
//e.g.: <cNode id="39" label="Courses"/>
pr.println("<cNode id=\"" + nodeID + "\" label=\"" + xmlTagEncode(category) + "\"/>");
if (path.lastIndexOf("/") != -1) {
String parentPath = path.substring(0, path.lastIndexOf("/"));
if (nodesToParents.put(nodeID, parentPath) != null) System.out.println("DUPLICATE NODE!");
} else if ("top".equalsIgnoreCase(path)) nodesToParents.put(nodeID, "Top");
}
pr.println("</SetOfNodes>");
System.out.println("Saving DMOZ nn_links information");
pr.println("<SetOfcLinks>");
Iterator it = nodesToParents.keySet().iterator();
while (it.hasNext()) {
String nodeID = (String) it.next();
String parentPath = nodesToParents.get(nodeID);
//e.g.: <cLink sourceNodeId="39" targetNodeId="39"/>
pr.println("<cLink sourceNodeId=\"" + nodeID + "\" targetNodeId=\"" + pathToID.get(parentPath) + "\"/>");
}
pr.println("</SetOfcLinks>");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
private static String xmlTagEncode(String xmlToEncode) {
if (xmlToEncode == null) {
return null;
}
StringBuffer out = new StringBuffer(xmlToEncode.length());
for (int t = 0; t < xmlToEncode.length(); t++) {
char c = xmlToEncode.charAt(t);
switch (c) {
case ('&'):
out.append("&");
break;
case ('<'):
out.append("<");
break;
case ('>'):
out.append(">");
break;
default:
out.append(c);
}
}
return out.toString();
}
}
here is my text file:structure1.txt
Top/Science/Earth_Sciences/Atmospheric_Chemistry
Top/Science/Earth_Sciences/Atmospheric_Chemistry/Publications
Top/Science/Earth_Sciences/Atmospheric_Chemistry/Modeling
Top/Science/Earth_Sciences/Atmospheric_Chemistry/Education
Top/Science/Earth_Sciences/Atmospheric_Chemistry/Meetings_and_Organizations

