Java Code Examples for org.jaxen.XPath#selectNodes()

The following examples show how to use org.jaxen.XPath#selectNodes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthoringXml.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Based on method in XmlStringBuffer
 * @author rpembry
 * @author casong changed XmlStringBuffer to be org.w3c.dom compliance,
 * @author Ed Smiley [email protected] changed method signatures used Document
 *
 * @return a List of Nodes
 */
public final List selectNodes(Document document, String xpath)
{
  if (log.isDebugEnabled())
  {
    log.debug("selectNodes(String " + xpath + ")");
  }

  List result = new ArrayList();

  try
  {
    XPath path = new DOMXPath(xpath);
    result = path.selectNodes(document);
  }
  catch (JaxenException je)
  {
    log.error(je.getMessage(), je);
  }

  return result;
}
 
Example 2
Source File: XmlConfiguration.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public String getUniqueValue(String xPath) {
    SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();
    nsCtx.addNamespace("ns", serverNamespace);
    try {
        XPath xp = new AXIOMXPath(xPath);
        xp.setNamespaceContext(nsCtx);
        OMElement elem = builder.getDocumentElement();
        if (elem != null) {
            List nodeList = xp.selectNodes(elem);
            Object obj;
            if (!nodeList.isEmpty() && ((obj = nodeList.get(0)) != null)) {
                return ((OMElement) obj).getText();
            }
        }
    } catch (JaxenException e) {
        throw new RuntimeException("XPath expression " + xPath + " failed", e);
    }
    return null;
}
 
Example 3
Source File: XmlConfiguration.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public OMElement[] getElements(String xPath) {
    SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();
    nsCtx.addNamespace("ns", serverNamespace);
    try {
        XPath xp = new AXIOMXPath(xPath);
        xp.setNamespaceContext(nsCtx);
        OMElement elem = builder.getDocumentElement();
        if (elem != null) {
            List nodeList = xp.selectNodes(elem);
            return (OMElement[]) nodeList.toArray(new OMElement[nodeList.size()]);
        }
    } catch (JaxenException e) {
        throw new RuntimeException("XPath expression " + xPath + " failed", e);
    }
    return new OMElement[0];
}
 
Example 4
Source File: AuthoringXml.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Based on method in XmlStringBuffer
 * @author rpembry
 * @author casong changed XmlStringBuffer to be org.w3c.dom compliance,
 * @author Ed Smiley [email protected] changed method signatures used Document
 *
 * @return a List of Nodes
 */
public final List selectNodes(Document document, String xpath)
{
  if (log.isDebugEnabled())
  {
    log.debug("selectNodes(String " + xpath + ")");
  }

  List result = new ArrayList();

  try
  {
    XPath path = new DOMXPath(xpath);
    result = path.selectNodes(document);
  }
  catch (JaxenException je)
  {
    log.error(je.getMessage(), je);
  }

  return result;
}
 
Example 5
Source File: JaxenDemo.java    From tutorials with MIT License 6 votes vote down vote up
public List getAllTutorial() {
    try {
        FileInputStream fileIS = new FileInputStream(this.getFile());
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        Document xmlDocument = builder.parse(fileIS);

        String expression = "/tutorials/tutorial";

        XPath path = new DOMXPath(expression);
        List result = path.selectNodes(xmlDocument);
        return result;

    } catch (SAXException | IOException | ParserConfigurationException | JaxenException e) {
        e.printStackTrace();
        return null;
    }

}
 
Example 6
Source File: JaxenNsAwareXPathExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Map<String, String> extractXmlNamespaces(Node contextNode) throws JRException 
{
	Map<String, String> namespaces = new HashMap<String, String>();
	List<Node> nlist;
	String namespaceXPathString = "//namespace::node()";

	try
	{
		XPath xpath = new DOMXPath(namespaceXPathString);
		nlist = xpath.selectNodes(contextNode);
		
		for (int i = 0; i < nlist.size(); i++) 
		{
			Node node = nlist.get(i);
			if(node.getParentNode() != null && node.getParentNode().getPrefix() != null)
			{
				if (!namespaces.containsKey(node.getParentNode().getPrefix()))
				{
					namespaces.put(node.getParentNode().getPrefix(), node.getParentNode().getNamespaceURI());
				}
			}
		}
		
	} catch (JaxenException e)
	{
		throw 
			new JRException(
				EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
				new Object[]{namespaceXPathString},
				e);
	}
	
	return namespaces;
}
 
Example 7
Source File: KbCsvMojo.java    From Asqatasun with GNU Affero General Public License v3.0 5 votes vote down vote up
private List<String> getUrls(String url) throws JDOMException, JaxenException, IOException {
    SAXBuilder builder = new SAXBuilder();
    EntityResolver resolver = new XhtmlEntityResolver();
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    builder.setEntityResolver(resolver);
    Document document = builder.build(new URL(url));
    XPath xpath = new JDOMXPath("//*[@id='resultat']//*[@href]/@href");
    List<Attribute> results = xpath.selectNodes(document);
    List<String> urls = new ArrayList<String>();
    for (Attribute attr : results) {
        urls.add(attr.getValue());
    }
    return urls;
}
 
Example 8
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static List selectNodes(String expression, Object doc) {
	if (doc == null) return new ArrayList();
	List nodes = null;
	try {
		XPath xpath = new DOMXPath(expression);
		nodes = xpath.selectNodes(doc);
	} catch (JaxenException e) {
		return null;
	}
	return nodes;  
}
 
Example 9
Source File: XmlCode.java    From RADL with Apache License 2.0 5 votes vote down vote up
public <T> Iterable<T> multiple(String path, Class<T> returnType) {
  Collection<T> result = new ArrayList<>();
  try {
    XPath xpath = new DOMXPath(path);
    for (Entry<String, String> entry : namespaces.entrySet()) {
      xpath.addNamespace(entry.getKey(), entry.getValue());
    }
    for (Object found : xpath.selectNodes(getRoot())) {
      result.add(returnType.cast(found));
    }
  } catch (JaxenException e) {
    throw new RuntimeException(e);
  }
  return result;
}
 
Example 10
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static List selectNodes(String expression, Object doc) {
	if (doc == null) return new ArrayList();
	List nodes = null;
	try {
		XPath xpath = new DOMXPath(expression);
		nodes = xpath.selectNodes(doc);
	} catch (JaxenException e) {
		return null;
	}
	return nodes;  
}