xmlGUY
Posts: 1
Nickname: xmlguy
Registered: Mar, 2002
|
|
Re: how to read write update a xml file.
|
Posted: Mar 17, 2002 5:40 PM
|
|
Well I didn't feel much like doing more than this for you.. sorry. But this gives you a great start as far as I'm concerned....I can;t remember how many methods are in this, but some of them seem to be complete junk, yet they work. So, for those of you out there snickering at my code.. this is not in any production environment. So........
import java.io.*; import java.util.*; import java.lang.Exception; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.DOMImplementation; import org.w3c.dom.DocumentType; import javax.xml.parsers.*;
public class xmlRPT {
int NoChild=0;
FileWriter out; File xmlOut;
Node rootElement = null; Element agenda = null; Element date = null; Element title=null; Element text = null; Document doc= null;
public void setValues()
{ try {
xmlOut= new File("agenda.xml");
if (xmlOut.exists()==false){ //if the document doesnt exist we will write all the header stuff.. and the root tag."PAGE" out = new FileWriter("report.xml",true); out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); out.write("<REPORT>"); //then we will start writing the agenda tag and child elements. printDOMTree(agenda, out);
} else{ //create a new filewriter to write to your XML file out = new FileWriter("report.xml",true);
//we will print the elements in memory starting with Agenda. not with Page. printDOMTree(agenda ,out);
} xmlOut = null; out.flush(); out.close(); out=null; System.gc(); }catch(IOException ioe) { System.out.println(ioe); } catch(Exception e){ System.out.println(e);
}
}
public void prepRpt(int onoff){ try { out = new FileWriter("report.xml",true);
if (onoff==0){
out = new FileWriter("report.xml",true); out.write("\n</AGENDA>"); out.flush(); out.close(); out = null; } else { if (onoff==1){
RandomAccessFile raf = new RandomAccessFile("agenda.xml", "rw"); long lenFile =raf.getFilePointer(); //this next line will start writing 9 bytes before the end of the line.. long putFile = lenFile + raf.length() - 9;
raf.seek(putFile); raf.writeUTF(" ");
raf.close();
raf=null; } } System.gc();
} catch(IOException ioe) { System.out.println(ioe); } catch(Exception e){ System.out.println(e);
}
}
public void parseOut(String values[]) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); doc = impl.createDocument( "", "PAGE", null); } catch (ParserConfigurationException cnfe) { System.out.println(cnfe); }
if (doc != null) { // Find the implementation
// Create the document
// Fill the document rootElement = doc.getDocumentElement(); agenda = doc.createElement("AGENDA"); title = doc.createElement("TITLE"); title.appendChild(doc.createTextNode("TITLE HERE")); agenda.appendChild(title);
date = doc.createElement("DATE"); date.appendChild(doc.createTextNode("DATE HERE")); agenda.appendChild(date);
text = doc.createElement("TEXT"); text.appendChild(doc.createTextNode("TEXT HERE")); agenda.appendChild(text);
rootElement.appendChild(agenda);
} }
/** Prints the specified node, recursively. */ public void printDOMTree(Node node, FileWriter out) throws IOException
{ int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { printDOMTree(((Document)node).getDocumentElement(), out); break; }
// print element with attributes case Node.ELEMENT_NODE: { if (node.getNodeName()=="EMPLOYEE"){ out.write("\n<");} else{ out.write("\n <"); } out.write(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); out.write(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } out.write(">");
NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++){
printDOMTree(children.item(i), out);} }
break; }
// handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { out.write("&"); out.write(node.getNodeName()); out.write(";"); break; }
// print cdata sections case Node.CDATA_SECTION_NODE: { out.write("<![CDATA["); out.write(node.getNodeValue()); out.write("]]>"); break; }
// print text case Node.TEXT_NODE: { out.write(node.getNodeValue()); break; }
// print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { out.write("<?"); out.write(node.getNodeName()); String data = node.getNodeValue(); { out.write(" "); out.write(data); } out.write("?>"); break; } }
if (type == Node.ELEMENT_NODE) { if (node.getNodeName()=="EMPLOYEE"){ out.write("\n"); } out.write("</"); out.write(node.getNodeName()); out.write('>'); } } // printDOMTree(Node, PrintWriter)
}
|
|