If you want to just use SAX to make this type of simple change, here is the code:
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;
import org.xml.sax.helpers.XMLReaderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class XMLPipeline {
public static void main(String[] args) throws Exception {
String inputFile = "a.xml";
PrintStream outputStream = System.out;
new XMLPipeline().process(inputFile, outputStream);
}
//default JDK
public void process(String inputFile, OutputStream outputStream) throws
SAXException, ParserConfigurationException, IOException, TransformerException {
StreamResult xwriter = new StreamResult(outputStream);
XMLReader xreader = XMLReaderFactory.createXMLReader();
XMLAnalyzer analyzer = new XMLAnalyzer(xreader);
TransformerFactory stf = SAXTransformerFactory.newInstance();
SAXSource ss = new SAXSource(analyzer, new InputSource(inputFile));
stf.newTransformer().transform(ss, xwriter);
}
public static class XMLAnalyzer extends XMLFilterImpl {
private Set<String> tags = new HashSet<String>(Arrays.asList("account", "city", "state"));
boolean bufferText = false;
StringBuilder buffer;
public XMLAnalyzer(XMLReader xmlReader) {
super(xmlReader);
}
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
super.startElement(uri, localName, qName, atts);
if (tags.contains(localName)) {
buffer = new StringBuilder("='");
bufferText = true;
}
}
@Override
public void characters(char[] chars, int i, int i1) throws SAXException {
if (bufferText) {
buffer.append(chars, i, i1);
} else {
super.characters(chars, i, i1);
}
}
@Override
public void endElement(String s, String s1, String s2) throws SAXException {
if (bufferText) {
buffer.append('\'');
super.characters(buffer.toString().toCharArray(), 0, buffer.length());
bufferText = false;
}
super.endElement(s, s1, s2);
}
}
}