Search in sources :

Example 21 with XPathFactory

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);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) IOException(java.io.IOException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File)

Example 22 with XPathFactory

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;
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 23 with XPathFactory

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;
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 24 with XPathFactory

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;
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 25 with XPathFactory

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("----------------------------------");
    }
}
Also used : XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) FileReader(java.io.FileReader)

Aggregations

XPathFactory (javax.xml.xpath.XPathFactory)75 XPath (javax.xml.xpath.XPath)59 XPathExpression (javax.xml.xpath.XPathExpression)40 Document (org.w3c.dom.Document)34 NodeList (org.w3c.dom.NodeList)34 XPathExpressionException (javax.xml.xpath.XPathExpressionException)26 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 Node (org.w3c.dom.Node)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 InputSource (org.xml.sax.InputSource)16 Test (org.junit.Test)15 IOException (java.io.IOException)13 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 Path (java.nio.file.Path)11 SAXException (org.xml.sax.SAXException)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)7