package xml;
import java.io.*;
import futils.*;
import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.*;
public class ModifXmlPrices {
static final int ELEMENT_NODE = 1;
static final int TEXT_NODE = 3;
static final int DOCUMENT_NODE = 9;
static float incr = 0;
static BufferedWriter out = null;
public static void main (String[] args)
throws IOException {
System.out.println("Enter percent price increase: ");
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
incr = new Float(in.readLine()).floatValue() * .01f;
File inFile = Futil.getReadFile("select an XML file");
File outFile = new File(inFile.getParent() + "\\NewProduct.xml");
out = new BufferedWriter(new FileWriter(outFile));
parse("file:" + inFile.getAbsolutePath());
out.flush();
out.close();
}
private static void parse (String uri)
throws IOException {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new MyErrorHandler());
Document doc = db.parse(uri);
Element root = doc.getDocumentElement();
traverse(root);
printNode(doc);
}
catch (ParserConfigurationException e) {
System.out.println(
"** ParserConfigurationException\n"
+ e.getMessage());
}
catch (SAXException e) {
System.out.println(
"** SAXException\n"
+ e.getMessage());
Exception ex = e.getException();
if (ex != null)
ex.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
private static void traverse (Node elem) {
try {
if (elem.hasChildNodes()) {
NodeList children = elem.getChildNodes();
int length = children.getLength();
for (int i=0; i<length; i++) {
Node n = children.item(i);
String name = n.getNodeName();
if (n.getNodeType() == ELEMENT_NODE) {
traverse(n); }
else
if (n.getNodeType() == TEXT_NODE) {
String pname = n.getParentNode().getNodeName();
if (pname.equals("UnitPrice")) {
String price = n.getNodeValue();
System.out.println(pname + " = " + price);
float f = (new Float(price).floatValue()) * (1 + incr);
int newPrice = new Float(f).intValue();
n.setNodeValue(String.valueOf(newPrice));
System.out.println("new value of " + pname +
" = " + n.getNodeValue() + "\n");
return;
}
}
}
return;
}
}
catch (DOMException e) {
System.out.println ("*** DOMException\n"
+ e.getMessage());
}
}
private static void printNode(Node node)
throws IOException {
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
out.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\""
+ "standalone=\"yes\"?>\n\n");
Document doc = (Document)node;
out.write ("<!DOCTYPE "
+ doc.getDocumentElement().getNodeName()
+ " [" + doc.getDoctype().getInternalSubset()
+ "]>\n");
NodeList nodes = node.getChildNodes();
if (nodes != null) {
for (int i=0; i<nodes.getLength(); i++) {
printNode(nodes.item(i));
}
}
break;
case Node.ELEMENT_NODE:
String name = node.getNodeName();
out.write("<" + name);
NamedNodeMap attributes = node.getAttributes();
for (int i=0; i<attributes.getLength(); i++) {
Node attr = attributes.item(i);
out.write(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() +
"\"");
out.newLine();
}
out.write(">");
NodeList children = node.getChildNodes();
if (children != null) {
for (int i=0; i<children.getLength(); i++)
printNode(children.item(i));
}
out.write("</" + name + ">");
case Node.TEXT_NODE:
String val = node.getNodeValue();
if (val != null)
out.write(val);
break;
}
}
private static class MyErrorHandler implements ErrorHandler {
private String getParseExceptionInfo(SAXParseException spe) {
String systemId = spe.getSystemId();
if (systemId == null) {
systemId = "null";
}
String info = "URI=" + systemId +
"\nLine=" + spe.getLineNumber() +
"\n" + spe.getMessage();
return info;
}
public void warning(SAXParseException spe) throws SAXException {
System.out.println("Warning: " + getParseExceptionInfo(spe));
}
public void error(SAXParseException spe) throws SAXException {
String message = "Error: " + getParseExceptionInfo(spe);
throw new SAXException(message);
}
public void fatalError(SAXParseException spe) throws SAXException {
String message = "Fatal Error: " + getParseExceptionInfo(spe);
throw new SAXException(message);
}
}
}