Search in sources :

Example 66 with XPathFactory

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

the class libraryDOM 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("//*", new InputSource(new FileReader("library.xml")), XPathConstants.NODESET);
        System.out.println("Nombre d'�l�ments : " + list.getLength());
        int nombreAtt = 0;
        for (int i = 0; i < list.getLength(); i++) {
            nombreAtt += list.item(i).getAttributes().getLength();
        }
        System.out.println("Nombre Attribut : " + nombreAtt);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) FileReader(java.io.FileReader)

Example 67 with XPathFactory

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

the class DOMA 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("//*", new InputSource(new FileReader("library.xml")), XPathConstants.NODESET);
    int compteurElem = lists.getLength();
    int compteurAtt = 0;
    for (int i = 0; i < lists.getLength(); i++) {
        compteurAtt += ((Element) lists.item(i)).getAttributes().getLength();
    }
    System.out.println("Nombre d'�l�ments : " + compteurElem);
    System.out.println("Nombre d'attributs : " + compteurAtt);
}
Also used : XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) FileReader(java.io.FileReader)

Example 68 with XPathFactory

use of javax.xml.xpath.XPathFactory in project GDSC-SMLM by aherbert.

the class BatchPeakFit method itemStateChanged.

/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
	 */
public void itemStateChanged(ItemEvent e) {
    // When the checkbox is clicked, create a default configuration file and update the
    // GenericDialog with the file location.		
    Checkbox cb = (Checkbox) e.getSource();
    if (cb.getState()) {
        cb.setState(false);
        Document doc = getDefaultSettingsXmlDocument();
        if (doc == null)
            return;
        try {
            // Look for nodes that are part of the fit configuration
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile("//gdsc.smlm.engine.FitEngineConfiguration//*");
            // For each node, add the name and value to the BatchParameters
            BatchSettings batchSettings = new BatchSettings();
            batchSettings.resultsDirectory = System.getProperty("java.io.tmpdir");
            batchSettings.images.add("/path/to/image.tif");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (// Only nodes with a single text entry
                node.getChildNodes().getLength() == 1) {
                    batchSettings.parameters.add(new ParameterSettings(node.getNodeName(), node.getTextContent()));
                }
            }
            // Save the settings file
            String[] path = Utils.decodePath(configFilenameText.getText());
            OpenDialog chooser = new OpenDialog("Settings_file", path[0], path[1]);
            if (chooser.getFileName() != null) {
                String newFilename = chooser.getDirectory() + chooser.getFileName();
                if (!newFilename.endsWith(".xml"))
                    newFilename += ".xml";
                FileOutputStream fs = null;
                try {
                    fs = new FileOutputStream(newFilename);
                    xs.toXML(batchSettings, fs);
                } finally {
                    if (fs != null) {
                        fs.close();
                    }
                }
                // Update dialog filename
                configFilenameText.setText(newFilename);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) ParameterSettings(gdsc.smlm.ij.settings.ParameterSettings) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) XStreamException(com.thoughtworks.xstream.XStreamException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) OpenDialog(ij.io.OpenDialog) XPathFactory(javax.xml.xpath.XPathFactory) BatchSettings(gdsc.smlm.ij.settings.BatchSettings) Checkbox(java.awt.Checkbox) FileOutputStream(java.io.FileOutputStream)

Example 69 with XPathFactory

use of javax.xml.xpath.XPathFactory in project cloudstack by apache.

the class OvmObject method xmlToMap.

/* was String, Object before */
public <E> Map<String, E> xmlToMap(String path, Document xmlDocument) throws Ovm3ResourceException {
    XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {
        XPathExpression xPathExpression = xPath.compile(path);
        NodeList nodeList = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
        Map<String, E> myMap = new HashMap<String, E>();
        for (int ind = 0; ind < nodeList.getLength(); ind++) {
            NodeList nodeListFor = nodeList.item(ind).getChildNodes();
            for (int index = 0; index < nodeListFor.getLength(); index++) {
                String rnode = nodeListFor.item(index).getNodeName();
                NodeList nodeListFor2 = nodeListFor.item(index).getChildNodes();
                if (nodeListFor2.getLength() > 1) {
                /* Do we need to figure out all the sub elements here and put them in a map? */
                } else {
                    String element = nodeListFor.item(index).getTextContent();
                    myMap.put(rnode, (E) element);
                }
            }
        }
        return myMap;
    } catch (XPathExpressionException e) {
        throw new Ovm3ResourceException("Problem parsing XML to Map:", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList)

Example 70 with XPathFactory

use of javax.xml.xpath.XPathFactory in project cloudstack by apache.

the class OvmObject method xmlToString.

public String xmlToString(String path, Document xmlDocument) throws Ovm3ResourceException {
    XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {
        XPathExpression xPathExpression = xPath.compile(path);
        NodeList nodeList = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
        return nodeList.item(0).getTextContent();
    } catch (NullPointerException e) {
        LOGGER.info("Got no items back from parsing, returning null: " + e);
        return null;
    } catch (XPathExpressionException e) {
        throw new Ovm3ResourceException("Problem parsing XML to String: ", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList)

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