java类javax.xml.xpath.XPathConstants的实例源码

JSONToXMLConverterTest.java 文件源码 项目:xrd4j 阅读 20 收藏 0 点赞 0 评论 0
/**
 * Test converting nested elements.
 */
public void testNestedElements2() throws XPathExpressionException {
    String json = "{\"menu\": {\"id\": \"file\",\"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";
    String xml = this.converter.convert(json);

    Document xmlConverted = SOAPHelper.xmlStrToDoc(xml);

    XPath xPath = XPathFactory.newInstance().newXPath();
    assertEquals("file", xPath.compile("/menu/id").evaluate(xmlConverted));
    assertEquals("File", xPath.compile("/menu/value").evaluate(xmlConverted));

    NodeList nodeList = (NodeList) xPath.compile("/menu/popup/menuitem").evaluate(xmlConverted, XPathConstants.NODESET);
    for (int i = 0; i < nodeList.getLength(); i++) {
        for (int j = 0; j < nodeList.item(i).getChildNodes().getLength(); j += 2) {
            String nodeName1 = nodeList.item(i).getChildNodes().item(j).getLocalName();
            String nodeValue1 = nodeList.item(i).getChildNodes().item(j).getTextContent();
            String nodeName2 = nodeList.item(i).getChildNodes().item(j + 1).getLocalName();
            String nodeValue2 = nodeList.item(i).getChildNodes().item(j + 1).getTextContent();

            if (!((nodeValue1.equals("New") && nodeValue2.equals("CreateNewDoc()")) || (nodeValue2.equals("New") && nodeValue1.equals("CreateNewDoc()")))
                    && !((nodeValue1.equals("Open") && nodeValue2.equals("OpenDoc()")) || (nodeValue2.equals("Open") && nodeValue1.equals("OpenDoc()")))
                    && !((nodeValue1.equals("Close") && nodeValue2.equals("CloseDoc()")) || (nodeValue2.equals("Close") && nodeValue1.equals("CloseDoc()")))) {
                fail();
            }
        }
    }
}
Pom.java 文件源码 项目:nbreleaseplugin 阅读 25 收藏 0 点赞 0 评论 0
private Element findElement(ElementValue pev, String path) {
    XPath xpath = XPathFactory.newInstance().newXPath();
    try {
        return (Element) xpath.evaluate(path, pev.getElement(), XPathConstants.NODE);
    } catch (XPathExpressionException ex) {
        return null;
    }
}
SelectMultipleFromNodeImpl.java 文件源码 项目:fluentxml4j 阅读 21 收藏 0 点赞 0 评论 0
@Override
public NodeList asNodeList()
{
    try
    {
        return (NodeList) this.xPathExpression.evaluate(this.baseNode, XPathConstants.NODESET);
    }
    catch (XPathException ex)
    {
        throw new FluentXmlProcessingException(ex);
    }
}
Bug4991857.java 文件源码 项目:openjdk-jdk10 阅读 15 收藏 0 点赞 0 评论 0
@Test
public void testXPath10() throws Exception {
    try {
        XPath xpath = xpathFactory.newXPath();
        Assert.assertNotNull(xpath);

        xpath.evaluate(".", d, XPathConstants.STRING);
        Assert.fail("XPathExpressionException not thrown");
    } catch (XPathExpressionException e) {
        // Expected exception as context node is null
    }
}
MetaDataMerger.java 文件源码 项目:neoscada 阅读 21 收藏 0 点赞 0 评论 0
private boolean isCategoryUnit ( final Element unit ) throws Exception
{
    final Object value = this.isCategoryPathExpression.evaluate ( unit, XPathConstants.STRING );
    if ( value == null )
    {
        return false;
    }
    return Boolean.parseBoolean ( (String)value );
}
ResXmlPatcher.java 文件源码 项目:AndroidApktool 阅读 22 收藏 0 点赞 0 评论 0
/**
 * Any @string reference in a <provider> value in AndroidManifest.xml will break on
 * build, thus preventing the application from installing. This is from a bug/error
 * in AOSP where public resources cannot be part of an authorities attribute within
 * a <provider> tag.
 *
 * This finds any reference and replaces it with the literal value found in the
 * res/values/strings.xml file.
 *
 * @param file File for AndroidManifest.xml
 * @throws AndrolibException
 */
public static void fixingPublicAttrsInProviderAttributes(File file) throws AndrolibException {
    if (file.exists()) {
        try {
            Document doc = loadDocument(file);
            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression expression = xPath.compile("/manifest/application/provider");

            Object result = expression.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;

            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                NamedNodeMap attrs = node.getAttributes();

                if (attrs != null) {
                    Node provider = attrs.getNamedItem("android:authorities");

                    if (provider != null) {
                        String reference = provider.getNodeValue();
                        String replacement = pullValueFromStrings(file.getParentFile(), reference);

                        if (replacement != null) {
                            provider.setNodeValue(replacement);
                            saveDocument(file, doc);
                        }
                    }
                }
            }

        }  catch (SAXException | ParserConfigurationException | IOException |
                XPathExpressionException | TransformerException ignored) {
        }
    }
}
ResXmlPatcher.java 文件源码 项目:AndroidApktool 阅读 27 收藏 0 点赞 0 评论 0
/**
 * Finds key in strings.xml file and returns text value
 *
 * @param directory Root directory of apk
 * @param key String reference (ie @string/foo)
 * @return String|null
 * @throws AndrolibException
 */
public static String pullValueFromStrings(File directory, String key) throws AndrolibException {
    if (! key.contains("@")) {
        return null;
    }

    File file = new File(directory, "/res/values/strings.xml");
    key = key.replace("@string/", "");

    if (file.exists()) {
        try {
            Document doc = loadDocument(file);
            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression expression = xPath.compile("/resources/string[@name=" + '"' + key + "\"]/text()");

            Object result = expression.evaluate(doc, XPathConstants.STRING);

            if (result != null) {
                return (String) result;
            }

        }  catch (SAXException | ParserConfigurationException | IOException | XPathExpressionException ignored) {
        }
    }

    return null;
}
TableFunctions.java 文件源码 项目:JATS2LaTeX 阅读 18 收藏 0 点赞 0 评论 0
protected static void cellParsing(XPath xPath, Table table, Node tableHead, String rowType)
        throws XPathExpressionException, NumberFormatException, DOMException {
    if (tableHead != null) {
        NodeList trHeadNodes = (NodeList) xPath.compile("tr").evaluate(tableHead, XPathConstants.NODESET);
        for (int y = 0; y < trHeadNodes.getLength(); y++) {
            Row row = new Row();
            row.setType(rowType);
            table.getRow().add(row);
            NodeList cellNodes = (NodeList) xPath.compile("th|td").evaluate(trHeadNodes.item(y), XPathConstants.NODESET);
            for (int w = 0; w < cellNodes.getLength(); w++) {
                Cell cell = new Cell();
                row.getCell().add(cell);
                Node cellNode = cellNodes.item(w);
                if (cellNode != null && cellNode.getAttributes() != null) {
                    if (cellNode.getAttributes().getNamedItem("colspan") != null) {
                        cell.setColspan(Integer.parseInt(cellNode.getAttributes().getNamedItem("colspan").getNodeValue()));
                    } else {
                        cell.setColspan(1);
                    }
                    if (cellNode.getAttributes().getNamedItem("rowspan") != null) {
                        cell.setRowspan(Integer.parseInt(cellNode.getAttributes().getNamedItem("rowspan").getNodeValue()));
                    } else {
                        cell.setRowspan(1);
                    }
                    ParContent parContent = new ParContent();
                    parContent.setType("tableCell");
                    cell.getParContent().add(parContent);
                    Body.parsingParContent(cellNode, parContent);
                } 

            }

        }
    }
}
EasyTravelScenarioService.java 文件源码 项目:playground-scenario-generator 阅读 21 收藏 0 点赞 0 评论 0
private NodeList getNodeListForDocAndExpression(String objectString, String expression) throws Exception {
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    InputSource source = new InputSource(new StringReader(objectString));
    Document doc = builder.parse(source);
    XPath xpath = xPathFactory.newXPath();
    return (NodeList) xpath.compile(expression).evaluate(doc, XPathConstants.NODESET);
}
XMLConverter.java 文件源码 项目:oscm 阅读 37 收藏 0 点赞 0 评论 0
/**
 * Returns the node in the given document at the specified XPath.
 * 
 * @param node
 *            The document to be checked.
 * @param xpathString
 *            The xpath to look at.
 * @return The node at the given xpath.
 * @throws XPathExpressionException
 */
public static Node getNodeByXPath(Node node, String xpathString)
        throws XPathExpressionException {

    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new XmlNamespaceResolver(
            getOwningDocument(node)));
    final XPathExpression expr = xpath.compile(xpathString);
    return (Node) expr.evaluate(node, XPathConstants.NODE);
}


问题


面经


文章

微信
公众号

扫码关注公众号