您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何解析XML以检索嵌入式文本节点

如何解析XML以检索嵌入式文本节点

感谢Marcin Krol,我找到了一个例子并得到了它:

static List<String> parseScenarioByGist() {
    Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
    String path = "/Users/haddad/development/industry/generalatomics/v1/gists_xml/AbsoluteValue.xml";

    try {
        Document doc = getDocument(path);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        XPathExpression expr = xpath.compile("/*/Scenarios/Scenario/ID");
        NodeList nList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        Integer count = 1;
        List<String> list = new ArrayList<String>();
        for(int i=0;i<nList.getLength();i++) {
            Node n = nList.item(i);
            System.out.println("Node Name: "+n.getTextContent());
            list.add(n.getNodeName());
            count++;
        }
        return list;
    }


    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}

要清理它,但效果很好。

其他 2022/1/1 18:32:47 有486人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶