如何解析XML以检索嵌入的文本节点 [英] how to parse XML to retrieve embedded text node

查看:102
本文介绍了如何解析XML以检索嵌入的文本节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析这个XML文件:

I'd like to parse this XML file:

要解析的XML文件:

<?xml version="1.0"?>
<Gist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../schema/Gist.xsd">
    <Name>AbsoluteValue</Name>
    <Description>Determines the absolute value.</Description>
    <PertinentData />
    <Scenarios>
        <Scenario>
            <ID>CALCULATED</ID>
            <Description>The value was used in the absolute value function.</Description>
            <Template>
                <NodeName />
                <Text> is </Text>
                <NodeValue />
                <Text> because it is the absolute value of </Text>
                <InputNameAsLink>Value</InputNameAsLink>
                <Text> (</Text>
                <InputValueAsLink>Value</InputValueAsLink>
                <Text>).</Text>             
            </Template>
        </Scenario>       
        <Scenario>
            <ID>INPUT_IS_BLANK</ID>
            <Description>The value is blank.</Description>
            <Template>
                <NodeName />
                <Text> is blank since </Text>
                <InputNameAsLink>Value</InputNameAsLink>
                <Text> (</Text>
                <InputValueAsLink>Value</InputValueAsLink>
                <Text>) is blank.</Text>
            </Template>
        </Scenario>
    </Scenarios>
</Gist>

我想给我回复一下Gist Name和场景。所以对于示例文件,我想要AbsoluteValue和一个场景列表(CALCULATED和INPUT_IS_BLANK)。如果我错了,请纠正我,但要使用的数据结构是

I'd like returned to me the the Gist Name, and scenarios. So for the example file, I'd like AbsoluteValue and a list of scenarios (CALCULATED and INPUT_IS_BLANK). Correct me if I'm wrong, but the data structure to use would be

Map<String, List<String>>

如何在Java代码中完成此操作?如果可能的话我想使用XPATH。

How can I accomplish this in Java code? I'd like to use XPATHs if possible.

我以为这是一个合适的XPATH表达式来获取每个SCENARIO?

I was thinking this would be a proper XPATH expression to get each SCENARIO?

/*/Scenarios/Scenario/ID/*


推荐答案

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

Thanks to Marcin Krol, I found an example and got it:

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;
}

要清理它,但它可以正常工作。

Gotta clean this up but it works nicesly.

这篇关于如何解析XML以检索嵌入的文本节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆