JSONToXMLConverterTest.java 文件源码

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

项目:xrd4j 作者:
/**
 * 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();
            }
        }
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号