简体   繁体   中英

how to parse XML to retrieve embedded text node

I'd like to parse this XML file:

XML file to parse:

<?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>

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

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

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

/*/Scenarios/Scenario/ID/*

The xPath to get Name:

/Gist/Name

To get ID

/Gist/Scenarios/Scenario/ID

To get both: Name ID

/Gist/Name | /Gist/Scenarios/Scenario/ID

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM