Java XPath Expression loadFromXmlNode(String xmlFile, String prefix)

Here you can find the source of loadFromXmlNode(String xmlFile, String prefix)

Description

load From Xml Node

License

Open Source License

Declaration

public static Properties loadFromXmlNode(String xmlFile, String prefix) throws Exception 

Method Source Code

//package com.java2s;
/**/*from w w w  .  j  a  v  a 2 s  . c  o  m*/
 * The MIT License (MIT)
 * Copyright (c) 2009-2015 HONG LEIMING
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;

import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Main {
    public static Properties loadFromXmlNode(String xmlFile, String prefix) throws Exception {
        XPath xpath = XPathFactory.newInstance().newXPath();
        InputSource source = new InputSource(xmlFile);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(source);

        XPathExpression expr = xpath.compile(prefix + "/*");
        NodeList list = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        Properties props = new Properties();
        if (list != null && list.getLength() > 0) {
            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);
                props.setProperty(node.getNodeName(), node.getTextContent());
            }
        }
        return props;
    }
}

Related

  1. findElements(final String xPathExpression, final Element root)
  2. hasAnimatedPng(ImageReader reader)
  3. isLegalXPath(final String xPath)
  4. isValidFilter(String xPathString)
  5. isXPathAbsolute(String path)
  6. loadNodeList(String file, String path)
  7. merge(XPathExpression expression, InputStream... files)
  8. modelPathExpr()
  9. parseXMLValue(String xml, String xpathExpression)