Is it possible to update an xml using a SAX parser.
As per my knowledge the sax parser loads the xml in chunks is it still possible to update the xml using sax if yes where can i find a sample example for the same.
My sample code for DOM which works on smaller files is below , I am unable to figure out how the following code needs to be changed so that it can work on files of size 18 MB
static List voList = new ArrayList();
public static void main(
String argv[]) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("c:\\xmlfiles\\first.xml"));
// normalize text representation
doc.getDocumentElement().normalize();
System.out.println("Root element of the doc is "
+ doc.getDocumentElement().getNodeName());
NodeList listOfBodies = doc
.getElementsByTagName("ShippedPtvDevice");
int totalBodies = listOfBodies.getLength();
System.out.println("Total no of Body Tags are : " + totalBodies);
for (int s = 0; s < listOfBodies.getLength(); s++) {
Node firstPersonNode = listOfBodies.item(s);
// MyIdsVo myIdVo = (MyIdsVo) voList.get(s);
if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
Element firstPersonElement = (Element) firstPersonNode;
NodeList firstNameList = firstPersonElement
.getElementsByTagName("PtvID");
Element firstNameElement = (Element) firstNameList.item(0);
// firstNameElement.setTextContent(myIdVo.getAuthCode());
NodeList lastNameList = firstPersonElement
.getElementsByTagName("IntegratedReceiverDeviceSN");
Element lastNameElement = (Element) lastNameList.item(0);
// lastNameElement.setTextContent(myIdVo.getDeviceID());
NodeList textLNList = lastNameElement.getChildNodes();
System.out
.println("Last Name : "
+ ((Node) textLNList.item(0))
.getNodeValue().trim());
}
// set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
// create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();
OutputStream f0;
byte buf[] = xmlString.getBytes();
f0 = new FileOutputStream("c:\\xmlfiles\\first.xml");
for (int i = 0; i < buf.length; i++) {
f0.write(buf[i]);
}
f0.close();
buf = null;
}// end of if clause
} catch (Exception e) {
System.out.println(e);
}
}
private void getParameters() {
String str, tmp, tmp1, tmp2, csvFileName = "c:\\Dummy_Devices_3_20000.prn";
StringTokenizer tok;
int lineCount = 0;
try {
FileInputStream fis = new FileInputStream(csvFileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
do {
str = br.readLine();
if (str == null) {
break;
}
lineCount++;
// taking tokens based on "," token separator and adding them to
// two vectors
tok = new StringTokenizer(str, ",");
try {
MyIdsVo vo = new MyIdsVo();
tmp = tok.nextToken();
vo.setAuthCode(tmp);
tmp1 = tok.nextToken();
vo.setDeviceID(tmp1);
tmp2 = tok.nextToken();
System.out.println(" " + tmp2);
voList.add(vo);
} catch (NoSuchElementException ex) {
System.out.println(ex);
}
} while (true);
fis.close();
} catch (FileNotFoundException fnofe) {
System.out.println(fnofe);
} catch (Exception e) {
System.out.println(e);
}
}