MarkupManipulator.java 文件源码

java
阅读 24 收藏 0 点赞 0 评论 0

项目:qpp-conversion-tool 作者:
public InputStream upsetTheNorm(String xPath, boolean remove) {
    try {
        document = dbf.newDocumentBuilder().parse(new File(pathname));
        XPath xpath = xpf.newXPath();
        XPathExpression expression = xpath.compile(xPath);

        NodeList searchedNodes = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
        if (searchedNodes == null) {
            System.out.println("bad path: " + xPath);
        } else {
            for (int i = 0; i < searchedNodes.getLength(); i++) {
                Node searched = searchedNodes.item(i);

                Node owningElement = (searched instanceof ElementImpl)
                        ? searched
                        : ((AttrImpl) searched).getOwnerElement();

                Node containingParent = owningElement.getParentNode();

                if (remove) {
                    containingParent.removeChild(owningElement);
                } else {
                    containingParent.appendChild(owningElement.cloneNode(true));
                }

            }
        }

        Transformer t = tf.newTransformer();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Result result = new StreamResult(os);
        t.transform(new DOMSource(document), result);
        return new ByteArrayInputStream(os.toByteArray());
    } catch (ParserConfigurationException | IOException | SAXException |
            XPathExpressionException | TransformerException ex) {
        throw new RuntimeException(ex);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号