Search in sources :

Example 1 with XOMXPath

use of org.jaxen.xom.XOMXPath in project vcell by virtualcell.

the class SEDMLUtils method parseXPath.

/**
 * Parses xPath string specified as arg1 in XML file specified as arg2 (using XOM and Jaxen)
 * @param xPathExpr
 * @param xmlFile
 * @return
 */
private static String parseXPath(String xPathExpr, File xmlFile) {
    try {
        Builder builder = new Builder();
        nu.xom.Document document = builder.build(xmlFile);
        nu.xom.Element rootElement = document.getRootElement();
        String defaultNamespaceURI = rootElement.getNamespaceURI();
        String defaultNamespacePrefix = rootElement.getNamespacePrefix();
        if ("".equals(defaultNamespacePrefix)) {
            ToolSupport.nameSpaces_PrefixesHashMap.put("sbml", defaultNamespaceURI);
        } else {
            ToolSupport.nameSpaces_PrefixesHashMap.put(defaultNamespacePrefix, defaultNamespaceURI);
        }
        org.jaxen.XPath xpath = new XOMXPath(xPathExpr);
        xpath.setNamespaceContext(new SimpleNamespaceContext(ToolSupport.nameSpaces_PrefixesHashMap));
        Object node = xpath.selectSingleNode(document);
        String elementId = ((nu.xom.Element) node).getAttributeValue("id");
        log.debug("Returned node id : " + elementId);
        return elementId;
    } catch (JaxenException e) {
        // An error occurred parsing or executing the XPath
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    } catch (IOException e) {
        // An error occurred opening the document
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    } catch (ParsingException e) {
        // An error occurred parsing the document
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    }
}
Also used : Builder(nu.xom.Builder) SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) IOException(java.io.IOException) XOMXPath(org.jaxen.xom.XOMXPath) JaxenException(org.jaxen.JaxenException) ParsingException(nu.xom.ParsingException) SimpleNamespaceContext(org.jaxen.SimpleNamespaceContext)

Aggregations

IOException (java.io.IOException)1 Builder (nu.xom.Builder)1 ParsingException (nu.xom.ParsingException)1 JaxenException (org.jaxen.JaxenException)1 SimpleNamespaceContext (org.jaxen.SimpleNamespaceContext)1 XOMXPath (org.jaxen.xom.XOMXPath)1 Element (org.jdom.Element)1 SAXBuilder (org.jdom.input.SAXBuilder)1