org.jaxen.dom.DOMXPath Java Examples

The following examples show how to use org.jaxen.dom.DOMXPath. 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: 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 #2
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 #3
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 #4
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 #5
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static String getNodeValue(String expression, Node node) {
	if (node == null) return "";
	String value = null;
	try {
		XPath xpath = new DOMXPath(expression);
		value = xpath.stringValueOf(node);
	} catch (JaxenException e) {
		return null;
	}
	return value;
}
 
Example #6
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static Node selectNode(String expression, Object doc) {
	if (doc == null) return null;
	Object node = null;
	try {
		XPath xpath = new DOMXPath(expression);
		node = xpath.selectSingleNode(doc);
	} catch (JaxenException e) {
		return null;
	}
	return (Node)node;  
}
 
Example #7
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 #8
Source File: XPath.java    From restcommander with Apache License 2.0 5 votes vote down vote up
private static DOMXPath getDOMXPath(String path, Map<String, String> namespaces) throws Exception {
    DOMXPath xpath = new DOMXPath(path);
    if (namespaces != null) {
        for (String prefix: namespaces.keySet()) {
            xpath.addNamespace(prefix, namespaces.get(prefix));
        }
    }
    return xpath;
}
 
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 String getNodeValue(String expression, Node node) {
	if (node == null) return "";
	String value = null;
	try {
		XPath xpath = new DOMXPath(expression);
		value = xpath.stringValueOf(node);
	} catch (JaxenException e) {
		return null;
	}
	return value;
}
 
Example #11
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static Node selectNode(String expression, Object doc) {
	if (doc == null) return null;
	Object node = null;
	try {
		XPath xpath = new DOMXPath(expression);
		node = xpath.selectSingleNode(doc);
	} catch (JaxenException e) {
		return null;
	}
	return (Node)node;  
}
 
Example #12
Source File: DomXpathSelector.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
public DomXpathSelector(String query) {
    super(query);
    try {
        this.xpath = new DOMXPath(query);
    } catch (JaxenException exception) {
        throw new RuntimeException(exception);
    }
}
 
Example #13
Source File: XmlUtils.java    From OpenEstate-IO with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link XPath} expression.
 *
 * @param expression      string with the XPath expression to create
 * @param doc             the document, whose namespace is bound to the XPath expression
 * @param namespacePrefix prefix of the document namespace, that is bound to the XPath expression
 * @return the created XPath expression
 * @throws JaxenException if the XPath is not creatable
 */
public static XPath newXPath(String expression, Document doc, String namespacePrefix) throws JaxenException {
    DOMXPath xpath = new DOMXPath(expression);
    //LOGGER.debug( "new xpath: " + xpath.debug() );
    if (doc != null && namespacePrefix != null) {
        Element root = XmlUtils.getRootElement(doc);
        String uri = StringUtils.trimToEmpty(root.getNamespaceURI());
        xpath.addNamespace(namespacePrefix, uri);
    }
    return xpath;
}
 
Example #14
Source File: MapFillComponent.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Float[] getCoords(String address) throws JRException {
	Float[] coords = null;
	if(address != null) {
		try {
			String reqParams = getReqParams();
			reqParams = reqParams != null && reqParams.trim().length() > 0 ? "&" + reqParams : reqParams;
			String urlStr = PLACE_URL_PREFIX + URLEncoder.encode(address, DEFAULT_ENCODING) + reqParams + PLACE_URL_SUFFIX;
			URL url = new URL(urlStr);
			byte[] response = JRLoader.loadBytes(url);
			Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(response));
			Node statusNode = (Node) new DOMXPath(STATUS_NODE).selectSingleNode(document);
			String status = statusNode.getTextContent();
			if(STATUS_OK.equals(status)) {
				coords = new Float[2];
				Node latNode = (Node) new DOMXPath(LATITUDE_NODE).selectSingleNode(document);
				coords[0] = Float.valueOf(latNode.getTextContent());
				Node lngNode = (Node) new DOMXPath(LONGITUDE_NODE).selectSingleNode(document);
				coords[1] = Float.valueOf(lngNode.getTextContent());
			} else {
				throw 
					new JRException(
						EXCEPTION_MESSAGE_KEY_ADDRESS_REQUEST_FAILED,  
						new Object[]{status} 
						);
			}
		} catch (Exception e) {
			throw new JRException(e);
		}
	}
	return coords;
}
 
Example #15
Source File: FillPlaceItem.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Float[] getCoords(String address) throws JRException {
	String reqParams = ((MapFillComponent)fillContextProvider).getReqParams();
	Float[] coords = null;
	if(address != null) {
		try {
			
			String urlStr = MapFillComponent.PLACE_URL_PREFIX 
					+ URLEncoder.encode(address, MapFillComponent.DEFAULT_ENCODING) 
					+ MapFillComponent.PLACE_URL_SUFFIX
					+ (reqParams != null && reqParams.trim().length() > 0 ? "&" + reqParams : "");

			URL url = new URL(urlStr);
			byte[] response = JRLoader.loadBytes(url);
			Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(response));
			Node statusNode = (Node) new DOMXPath(MapFillComponent.STATUS_NODE).selectSingleNode(document);
			String status = statusNode.getTextContent();
			if(MapFillComponent.STATUS_OK.equals(status)) {
				coords = new Float[2];
				Node latNode = (Node) new DOMXPath(MapFillComponent.LATITUDE_NODE).selectSingleNode(document);
				coords[0] = Float.valueOf(latNode.getTextContent());
				Node lngNode = (Node) new DOMXPath(MapFillComponent.LONGITUDE_NODE).selectSingleNode(document);
				coords[1] = Float.valueOf(lngNode.getTextContent());
			} else {
				throw 
					new JRException(
						MapFillComponent.EXCEPTION_MESSAGE_KEY_ADDRESS_REQUEST_FAILED,  
						new Object[]{status}
						);
			}
		} catch (Exception e) {
			throw new JRException(e);
		}
	}
	return coords;
}
 
Example #16
Source File: JaxenXPathExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Object selectObject(Node contextNode, String expression) throws JRException
{
	try
	{
		XPath xpath = new DOMXPath(expression);
		Object object = xpath.evaluate(contextNode);
		Object value;
		if (object instanceof List<?>)
		{
			List<?> list = (List<?>) object;
			if (list.isEmpty())
			{
				value = null;
			}
			else
			{
				value = list.get(0);
			}
		}
		else if (object instanceof Number || object instanceof Boolean)
		{
			value = object;
		}
		else
		{
			value = object.toString();
		}
		return value;
	}
	catch (JaxenException e)
	{
		throw 
			new JRException(
				EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
				new Object[]{expression},
				e);
	}
}
 
Example #17
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 #18
Source File: HMDBParser.java    From act with GNU General Public License v3.0 4 votes vote down vote up
DOMXPath compile() throws JaxenException {
  return new DOMXPath(this.getPath());
}
 
Example #19
Source File: PubchemParser.java    From act with GNU General Public License v3.0 4 votes vote down vote up
DOMXPath compile() throws JaxenException {
  return new DOMXPath(this.getPath());
}