use of javax.xml.transform.TransformerFactory in project azure-tools-for-java by Microsoft.
the class ParseXML method saveXMLDocument.
/**
* Save XML file and saves XML document.
*
* @param fileName
* @param doc
* @return boolean
* @throws Exception object
*/
static boolean saveXMLDocument(String fileName, Document doc) throws Exception {
// open output stream where XML Document will be saved
File xmlOutputFile = new File(fileName);
FileOutputStream fos = null;
Transformer transformer;
try {
fos = new FileOutputStream(xmlOutputFile);
// Use a Transformer for output
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fos);
// transform source into result will do save
transformer.transform(source, result);
} finally {
if (fos != null) {
fos.close();
}
}
return true;
}
use of javax.xml.transform.TransformerFactory in project azure-tools-for-java by Microsoft.
the class ParserXMLUtility method saveXMLFile.
/**
* save XML file and saves XML document.
*
* @param fileName
* @param doc
* @return XML document or <B>null</B> if error occurred
* @throws IOException
* @throws Exception
*/
public static boolean saveXMLFile(String fileName, Document doc) throws Exception {
File xmlFile = null;
FileOutputStream fos = null;
Transformer transformer;
try {
xmlFile = new File(fileName);
fos = new FileOutputStream(xmlFile);
TransformerFactory transFactory = TransformerFactory.newInstance();
transformer = transFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult destination = new StreamResult(fos);
// transform source into result will do save
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(source, destination);
} finally {
if (fos != null) {
fos.close();
}
}
return true;
}
use of javax.xml.transform.TransformerFactory in project azure-tools-for-java by Microsoft.
the class XMLHelper method main.
public static void main(String[] argv) {
if (argv.length < 4) {
System.out.println("[Failed] Please specify the arguments $XML_FILE $XPATH $NEW_VALUE $JOIN_OR_REPLACE.");
System.exit(1);
}
String FILEPATH = argv[0];
String XPATH = argv[1];
String NEW_VALUE = argv[2];
String CHANGETYPE = argv[3];
boolean DISPLAY_LOG = false;
if (argv.length > 4 && argv[4].toLowerCase().equals("true")) {
DISPLAY_LOG = true;
}
try {
System.out.println("Starting to modify XML " + FILEPATH);
// Read the content from xml file
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(FILEPATH);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
XPathExpression expression = xpath.compile(XPATH);
Node targetNode = (Node) expression.evaluate(doc, XPathConstants.NODE);
String oldValue = targetNode.getNodeValue();
String newValue = "";
if (CHANGETYPE.equals("JOIN")) {
newValue = oldValue + NEW_VALUE;
}
if (DISPLAY_LOG) {
System.out.println("The old value is " + oldValue);
System.out.println("The new value is " + newValue);
}
targetNode.setNodeValue(newValue);
// Write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(FILEPATH));
transformer.transform(source, result);
System.out.println("Modify XML Finished.");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.exit(0);
}
use of javax.xml.transform.TransformerFactory in project azure-tools-for-java by Microsoft.
the class AIParserXMLUtility method saveXMLFile.
/**
* save XML file and saves XML document.
*
* @param fileName
* @param doc
* @return XML document or <B>null</B> if error occurred
* @throws IOException
* @throws WindowsAzureInvalidProjectOperationException
*/
protected static boolean saveXMLFile(String fileName, Document doc) throws IOException, Exception {
File xmlFile = null;
FileOutputStream fos = null;
Transformer transformer;
try {
xmlFile = new File(fileName);
fos = new FileOutputStream(xmlFile);
TransformerFactory transFactory = TransformerFactory.newInstance();
transformer = transFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult destination = new StreamResult(fos);
// transform source into result will do save
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(source, destination);
} catch (Exception excp) {
Activator.getDefault().log(excp.getMessage(), excp);
throw new Exception(String.format("%s%s", Messages.saveErrMsg, excp.getMessage()));
} finally {
if (fos != null) {
fos.close();
}
}
return true;
}
use of javax.xml.transform.TransformerFactory in project lwjgl by LWJGL.
the class StandalonePublisher method doWriteDocument.
/**
* @param content
* @param sr
* @throws TransformerFactoryConfigurationError
* @throws TransformerConfigurationException
* @throws TransformerException
*/
private void doWriteDocument(Document content, StreamResult sr) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
Properties oprops = new Properties();
oprops.put(OutputKeys.METHOD, "xml");
oprops.put(OutputKeys.INDENT, "yes");
t.setOutputProperties(oprops);
t.transform(new DOMSource(content), sr);
}
Aggregations