Last active
February 19, 2024 21:29
-
-
Save libraplanet/7daff5c918cb2e531e2f7acdb5c720ef to your computer and use it in GitHub Desktop.
ModXml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Modify XML Value Using Java. | |
$ javac -encoding UTF-8 IOUtils.java ModXmlUtils.java ModXml.java | |
$ java ModXml -element "/hoge/gebu" -attribute "foo" -value "bar" -input "in.xml" -output "out.xml" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStream; | |
import java.io.OutputStream; | |
public final class IOUtils { | |
public static void closeQuietly(InputStream stream) { | |
if(stream != null) { | |
try { | |
stream.close(); | |
} catch(Exception e) { | |
} | |
} | |
} | |
public static void closeQuietly(OutputStream stream) { | |
if(stream != null) { | |
try { | |
stream.close(); | |
} catch(Exception e) { | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.ArrayList; | |
import java.io.File; | |
import java.io.InputStream; | |
import java.io.FileInputStream; | |
import java.io.OutputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.FileNotFoundException; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.TransformerException; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import javax.xml.xpath.XPath; | |
import javax.xml.xpath.XPathConstants; | |
import javax.xml.xpath.XPathFactory; | |
import javax.xml.xpath.XPathExpressionException; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.w3c.dom.Element; | |
import org.xml.sax.SAXException; | |
public class ModXml { | |
/** | |
* proc. | |
* | |
* @param paramElementXpath elementのXPath | |
* @param paramAttributes 属性名 | |
* @param paramOutputValue 出力値 | |
* @param paramInputXmlFileName 読み込みXMLファイル名 | |
* @param paramOutputXmlFileName 出力XMLファイル名 | |
* @param paramIsForceAttribute 属性強制出力 (対象elementに属性がない場合でも書き出し) | |
*/ | |
private static void proc(final String paramElementXpath, final String paramAttributes, final String paramOutputValue, final String paramInputXmlFileName, final String paramOutputXmlFileName, final boolean paramIsForceAttribute) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException { | |
final XPath xpath = XPathFactory.newInstance().newXPath(); | |
final Document document = ModXmlUtils.parseDocument(paramInputXmlFileName); | |
NodeList nodeList = (NodeList)xpath.evaluate(paramElementXpath, document, XPathConstants.NODESET); | |
if((nodeList == null) && (nodeList.getLength() == 0)) { | |
System.err.println(String.format("element is not found... '%s'", paramElementXpath)); | |
} else { | |
for(int i = 0; i < nodeList.getLength(); i++) { | |
final Node node = nodeList.item(i); | |
final Element element = (Element)node; | |
final String fullNodePath = ModXmlUtils.getFullNodePath(node); | |
if(paramAttributes == null) { | |
element.setTextContent(paramOutputValue); | |
System.out.println(String.format("set text content to '%s' for '%s'.", paramOutputValue, fullNodePath)); | |
} else { | |
if(paramIsForceAttribute) { | |
// 強制書き出し | |
element.setAttribute(paramAttributes, paramOutputValue); | |
System.out.println(String.format("set attribute to '%s' for '%s:%s'.", paramOutputValue, fullNodePath, paramAttributes)); | |
} else { | |
if(element.hasAttribute(paramAttributes)) { | |
element.setAttribute(paramAttributes, paramOutputValue); | |
System.out.println(String.format("set attribute to '%s' for '%s:%s'.", paramOutputValue, fullNodePath, paramAttributes)); | |
} else { | |
System.err.println(String.format("attribute is not found '%s:%s'.", fullNodePath, paramAttributes)); | |
} | |
} | |
} | |
} | |
} | |
ModXmlUtils.writeXml(paramOutputXmlFileName, document); | |
} | |
public static void main(final String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException { | |
boolean paramIsForceAttribute = false; | |
String paramElementXpath = null; | |
String paramAttributes = null; | |
String paramOutputValue = null; | |
String paramInputXmlFileName = null; | |
String paramOutputXmlFileName = null; | |
boolean requestHelp = false;; | |
boolean isError = false; | |
List<String> errors = new ArrayList<String>(); | |
for(int i = 0; i < args.length; i++) { | |
final String arg = args[i]; | |
final String param; | |
if((i + 1) < args.length) { | |
param = args[i + 1]; | |
} else { | |
param = null; | |
} | |
if(arg != null) { | |
if(arg.equals("--h") || arg.equals("-help")) { | |
requestHelp = true; | |
} else if(arg.equals("--f") || arg.equals("-force")) { | |
paramIsForceAttribute = true; | |
} else if(arg.equals("--e") || arg.equals("-element")) { | |
paramElementXpath = param; | |
i += 1; | |
} else if(arg.equals("--a") || arg.equals("-attribute")) { | |
paramAttributes = param; | |
i += 1; | |
} else if(arg.equals("--v") || arg.equals("-value")) { | |
paramOutputValue = param; | |
i += 1; | |
} else if(arg.equals("--i") || arg.equals("-input")) { | |
paramInputXmlFileName = param; | |
i += 1; | |
} else if(arg.equals("--o") || arg.equals("-output") ) { | |
paramOutputXmlFileName = param; | |
i += 1; | |
} | |
} | |
} | |
if(paramElementXpath == null) { | |
errors.add("no param at -element"); | |
isError = true; | |
} | |
if(paramAttributes == null) { | |
errors.add("no param at -attribute"); | |
isError = true; | |
} | |
if(paramOutputValue == null) { | |
errors.add("no param at -value"); | |
isError = true; | |
} | |
if(paramInputXmlFileName == null) { | |
errors.add("no param at -input"); | |
isError = true; | |
} | |
if(paramOutputXmlFileName == null) { | |
errors.add("no param at -output"); | |
isError = true; | |
} | |
if(requestHelp || isError) { | |
System.out.println("ModXml"); | |
System.out.println(); | |
System.out.println("USAGE: java ModXml [OPTION] -element [XPath] ( -attribute [ATTRIBUTE NAME] ) -output [FILE NAME] -input [FILE NAME]"); | |
System.out.println(" ARGS:"); | |
System.out.println(" --e, -element target element XPath."); | |
System.out.println(" --a, -attribute (optional) target attribute name. write value to this attribute, if defined."); | |
System.out.println(" --v, -value output value."); | |
System.out.println(" --i, -input input xml file name."); | |
System.out.println(" --o, -output output xml file name."); | |
System.out.println(); | |
System.out.println(" OPTIONS:"); | |
System.out.println(" --h, -help output help."); | |
System.out.println(" --f, -force force write value to attribute, if target attribute is not found on element."); | |
for(String mes : errors) { | |
System.out.println(mes); | |
} | |
} else { | |
proc(paramElementXpath, paramAttributes, paramOutputValue, paramInputXmlFileName, paramOutputXmlFileName, paramIsForceAttribute); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.ArrayList; | |
import java.io.File; | |
import java.io.InputStream; | |
import java.io.FileInputStream; | |
import java.io.OutputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.FileNotFoundException; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.TransformerException; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import javax.xml.xpath.XPath; | |
import javax.xml.xpath.XPathConstants; | |
import javax.xml.xpath.XPathFactory; | |
import javax.xml.xpath.XPathExpressionException; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.w3c.dom.Element; | |
import org.xml.sax.SAXException; | |
public class XmlUtils { | |
public static Document parseDocument(String fileName) throws FileNotFoundException, ParserConfigurationException, SAXException, IOException { | |
FileInputStream stream = null; | |
try { | |
stream = new FileInputStream(fileName); | |
return parseDocument(stream); | |
} finally { | |
IOUtils.closeQuietly(stream); | |
} | |
} | |
public static Document parseDocument(final InputStream iStream) throws ParserConfigurationException, SAXException, IOException { | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
Document document = builder.parse(iStream); | |
return document; | |
} | |
public static void writeXml(final String fileName, final Document document) throws FileNotFoundException, TransformerException { | |
FileOutputStream stream = null; | |
try { | |
stream = new FileOutputStream(fileName); | |
writeXml(stream, document); | |
} finally { | |
IOUtils.closeQuietly(stream); | |
} | |
} | |
public static void writeXml(final OutputStream oStream, final Document document) throws TransformerException { | |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |
Transformer transformer = transformerFactory.newTransformer(); | |
DOMSource domSource = new DOMSource(document); | |
StreamResult streamResult = new StreamResult(oStream); | |
transformer.transform(domSource, streamResult); | |
} | |
public static String getFullNodePath(final Node node) { | |
final Node parent = node.getParentNode(); | |
if(parent == null) { | |
return node.getNodeName(); | |
} else { | |
return getFullNodePath(parent) + "/" + node.getNodeName(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment