use of javax.xml.xpath.XPathFactory 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.xpath.XPathFactory in project tdi-studio-se by Talend.
the class Schema2XMLLinker method validateXPathExpression.
/**
* DOC amaumont Comment method "validateXPathExpression".
*
* @param newValue
* @return null if expression is valid, else return the error message.
*/
public String validateXPathExpression(String xpathExpression) {
if (xpathExpression == null || xpathExpression.trim().length() == 0) {
return null;
}
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
try {
xpath.compile(xpathExpression);
} catch (Exception e) {
//$NON-NLS-1$
return Messages.getString("XmlToXPathLinker.exceptionReturn.xPathInvalid");
}
return null;
}
use of javax.xml.xpath.XPathFactory in project tdi-studio-se by Talend.
the class JSONToXPathLinker method validateXPathExpression.
/**
* DOC amaumont Comment method "validateXPathExpression".
*
* @param newValue
* @return null if expression is valid, else return the error message.
*/
public String validateXPathExpression(String xpathExpression) {
if (xpathExpression == null || xpathExpression.trim().length() == 0) {
return null;
}
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
try {
xpath.compile(xpathExpression);
} catch (Exception e) {
return "The current XPath expression is invalid";
}
return null;
}
use of javax.xml.xpath.XPathFactory in project tdi-studio-se by Talend.
the class SchemaXMLLinker method validateXPathExpression.
/**
* DOC amaumont Comment method "validateXPathExpression".
*
* @param newValue
* @return null if expression is valid, else return the error message.
*/
public String validateXPathExpression(String xpathExpression) {
if (xpathExpression == null || xpathExpression.trim().length() == 0) {
return null;
}
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
try {
xpath.compile(xpathExpression);
} catch (Exception e) {
//$NON-NLS-1$
return Messages.getString("XmlToXPathLinker.exceptionReturn.xPathInvalid");
}
return null;
}
use of javax.xml.xpath.XPathFactory in project Synthese_2BIN by TheYoungSensei.
the class DOMB method main.
public static void main(String[] args) throws Exception {
XPathFactory factory = XPathFactory.newInstance();
javax.xml.xpath.XPath xPath = factory.newXPath();
NodeList lists = (NodeList) xPath.evaluate("//list", new InputSource(new FileReader("library.xml")), XPathConstants.NODESET);
for (int i = 0; i < lists.getLength(); i++) {
Element list = (Element) lists.item(i);
System.out.println(list.getAttribute("name"));
NodeList tracks = list.getElementsByTagName("track_ID");
for (int j = 0; j < tracks.getLength(); j++) {
Node artist = (Node) xPath.evaluate("//song[track_ID = " + ((Element) tracks.item(j)).getTextContent() + "]/artist", new InputSource(new FileReader("library.xml")), XPathConstants.NODE);
Node song = (Node) xPath.evaluate("//song[track_ID = " + ((Element) tracks.item(j)).getTextContent() + "]/name", new InputSource(new FileReader("library.xml")), XPathConstants.NODE);
System.out.println(artist.getTextContent() + " - " + song.getTextContent());
}
System.out.println("----------------------------------");
}
}
Aggregations