Search in sources :

Example 26 with XPathFactory

use of javax.xml.xpath.XPathFactory in project Synthese_2BIN by TheYoungSensei.

the class libraryDOM2 method main.

public static void main(String[] args) {
    try {
        XPathFactory factory = XPathFactory.newInstance();
        javax.xml.xpath.XPath xPath = factory.newXPath();
        NodeList list = (NodeList) xPath.evaluate("//list", new InputSource(new FileReader("library.xml")), XPathConstants.NODESET);
        for (int i = 0; i < list.getLength(); i++) {
            Element elem = (Element) list.item(i);
            System.out.println(elem.getAttribute("name"));
            NodeList tracks = elem.getElementsByTagName("track_ID");
            for (int j = 0; j < tracks.getLength(); j++) {
                Element track = (Element) tracks.item(j);
                Node artist = (Node) xPath.evaluate("//song[track_ID = " + track.getTextContent() + "]/artist", new InputSource(new FileReader("library.xml")), XPathConstants.NODE);
                Node name = (Node) xPath.evaluate("//song[track_ID = " + track.getTextContent() + "]/name", new InputSource(new FileReader("library.xml")), XPathConstants.NODE);
                System.out.println(artist.getTextContent() + " - " + name.getTextContent());
            }
            System.out.println("----------------------------------");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) FileReader(java.io.FileReader)

Example 27 with XPathFactory

use of javax.xml.xpath.XPathFactory in project jangaroo-tools by CoreMedia.

the class PomConverter method changePackaging.

/**
   * Changes the packaging from jangaroo to jangaroo-pkg in {@code /project/packaging}
   */
private static void changePackaging(Document document) throws MojoExecutionException {
    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();
        Node packagingNode = (Node) xPath.evaluate("/project/packaging[text() = 'jangaroo']", document, NODE);
        if (packagingNode != null) {
            packagingNode.setTextContent("jangaroo-pkg");
        }
    } catch (XPathException e) {
        throw new MojoExecutionException("error while generating modified POM", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) XPathException(javax.xml.xpath.XPathException) Node(org.w3c.dom.Node)

Example 28 with XPathFactory

use of javax.xml.xpath.XPathFactory in project jangaroo-tools by CoreMedia.

the class PomConverter method removeExmlPlugin.

/**
   * Replaces exml-maven-plugin configuration by jangaroo-maven-plugin configuration within
   * {@code /project/build/plugins} and {@code /project/build/pluginManagement/plugins}.
   */
private static void removeExmlPlugin(Document document) throws MojoExecutionException {
    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();
        Node pluginsNode = (Node) xPath.evaluate("/project/build/plugins", document, NODE);
        removeExmlPlugin(pluginsNode);
        pluginsNode = (Node) xPath.evaluate("/project/build/pluginManagement/plugins", document, NODE);
        removeExmlPlugin(pluginsNode);
    } catch (XPathException e) {
        throw new MojoExecutionException("error while generating modified POM", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) XPathException(javax.xml.xpath.XPathException) Node(org.w3c.dom.Node)

Example 29 with XPathFactory

use of javax.xml.xpath.XPathFactory in project perun by CESNET.

the class ExtSourceXML method getValueFromXpath.

/**
	 * Get xml Node and xpath expression to get value from node by this xpath.
	 *
	 * @param node node for getting value from
	 * @param xpathExpression expression for xpath to looking for value in node
	 * @return string extracted from node by xpath
	 * @throws InternalErrorException
	 */
protected String getValueFromXpath(Node node, String xpathExpression) throws InternalErrorException {
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr;
    try {
        expr = xpath.compile(xpathExpression);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when compiling xpath query.", ex);
    }
    String text;
    try {
        text = (String) expr.evaluate(node, XPathConstants.STRING);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when evaluate xpath query on node.", ex);
    }
    return text;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 30 with XPathFactory

use of javax.xml.xpath.XPathFactory in project perun by CESNET.

the class ExtSourceXML method xpathParsing.

/**
	 * Get query and maxResults.
	 * Prepare document and xpathExpression by query.
	 * Get all nodes by xpath from document and parse them one by one.
	 *
	 * The way of xml take from "file" or "uri" (configuration file)
	 *
	 * @param query xpath query from config file
	 * @param maxResults never get more than maxResults results (0 mean unlimited)
	 *
	 * @return List of results, where result is Map<String,String> like <name, value>
	 * @throws InternalErrorException
	 */
protected List<Map<String, String>> xpathParsing(String query, int maxResults) throws InternalErrorException {
    //Prepare result list
    List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
    //Create new document factory builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new InternalErrorException("Error when creating newDocumentBuilder.", ex);
    }
    Document doc;
    try {
        if (file != null && !file.isEmpty()) {
            doc = builder.parse(file);
        } else if (uri != null && !uri.isEmpty()) {
            doc = builder.parse(this.createTwoWaySSLConnection(uri));
        } else {
            throw new InternalErrorException("Document can't be parsed, because there is no way (file or uri) to this document in xpathParser.");
        }
    } catch (SAXParseException ex) {
        throw new InternalErrorException("Error when parsing uri by document builder.", ex);
    } catch (SAXException ex) {
        throw new InternalErrorException("Problem with parsing is more complex, not only invalid characters.", ex);
    } catch (IOException ex) {
        throw new InternalErrorException("Error when parsing uri by document builder. Problem with input or output.", ex);
    }
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression queryExpr;
    try {
        queryExpr = xpath.compile(query);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when compiling xpath query.", ex);
    }
    //Call query on document node and get back nodesets
    NodeList nodeList;
    try {
        nodeList = (NodeList) queryExpr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when evaluate xpath query on document.", ex);
    }
    //Test if there is any nodeset in result
    if (nodeList.getLength() == 0) {
        //There is no results, return empty subjects
        return subjects;
    }
    //Iterate through nodes and convert them to Map<String,String>
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node singleNode = nodeList.item(i);
        // remove node from original structure in order to keep access time constant (otherwise is exp.)
        singleNode.getParentNode().removeChild(singleNode);
        Map<String, String> map = convertNodeToMap(singleNode);
        if (map != null)
            subjects.add(map);
        //Reducing results by maxResults
        if (maxResults > 0) {
            if (subjects.size() >= maxResults)
                break;
        }
    }
    this.close();
    return subjects;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map)

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