tags:

views:

47

answers:

0

I have a DOM created with namespace 4 namespaces

I have namespace mappings set correctly and when I query with any expression that uses my namespace it returns empty.

The weird thing is that if I export the DOM to a string and then create a new DOM from there my expressions work like a charm.

How can I check that the DOM I'm creating is correct? Any ideas on how to fix this without having to export to String and the back to DOM?

String parseNodeToString(Node dom) {
    javax.xml.transform.Transformer transformer;
    StringWriter stringWriter = new StringWriter();
    try {

        transformer = TransformerFactory.newInstance().newTransformer();
        Source source = new DOMSource(dom);
        Result output = new StreamResult(stringWriter);
        transformer.transform(source, output);

    } catch (TransformerConfigurationException e) {
        logger.debug(e.getMessage());
    } catch (TransformerException e) {
        logger.debug(e.getMessage());
    }
    return stringWriter.getBuffer().toString();
}   

Then loading the document from the string

static Document loadDOMInputSource(InputSource inputSource) {
    Document inputSourceDom = null;
    DocumentBuilder documentBuilder = null;
    try {
        documentBuilder = XMLDocumentBuilderUtil.getDocumentBuilder();
        if (inputSource != null) {
            inputSourceDom = documentBuilder.parse(inputSource);
        } else {
            inputSourceDom = documentBuilder.newDocument();
        }

    } catch (SAXException saxe) {
        logger.error(saxe.getMessage(), saxe);
        logger.debug("Content is not xml"); //$NON-NLS-1$
        inputSourceDom = null;
    } catch (IOException ioe) {
        logger.error(ioe.getMessage(), ioe);
        inputSourceDom = null;
    } finally {
        if (documentBuilder != null) {
            XMLDocumentBuilderUtil.returnDocumentBuilder(documentBuilder);
        }
    }

    return inputSourceDom;
}

This is the xpath I'm using

entriesFrom = XPathParser.findNodes(
                                "//ds:artifact[contains(ds:about,\"" + id + "\")]", doc);

For XPathParser code

public class XPathParser {
public static NodeList findNodes(String path, Object src) throws XPathExpressionException {
    XPathExpression expr = xpath(path);
    return (NodeList) expr.evaluate(src, XPathConstants.NODESET);
}

private static XPathExpression xpath(String expr) throws XPathExpressionException  {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(NamespaceContext.getInstance());
    return xpath.compile(expr);
}

}

NamespaceContext extends IRRCNamespaceContext