Problem evaluating XPath expression in Java

Can someone help me find the error I am making when evaluating the following XPath expression?
I want to get all the "DataTable" nodes under the "Model" node in my xml through XPath.
Here is my XML document:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <Application>
    <Model>
      <DataSet name="ds" primaryTable="Members" openRows="1">
        <DataTable name="Members" openFor="write">
          <DataColumn name="id" type="String" mandatory="true" primaryKey="true" valueBy="user"/>
          <DataColumn name="name" type="String" mandatory="true" valueBy="user"/>
          <DataColumn name="address" type="String" mandatory="false" valueBy="user"/>
          <DataColumn name="city" type="String" mandatory="false" valueBy="user"/>
          <DataColumn name="country" type="String" mandatory="false" valueBy="user"/>
        </DataTable>
      </DataSet>
    </Model>
    <View>
      <Composite>
        <Grid>
          <Label value="ID:" row="0" column="0" />
          <Label value="Name:" row="1" column="0" />
          <Label value="Address:" row="2" column="0" />
          <Label value="City:" row="3" column="0" />
          <Label value="Country:" row="4" column="0" />

          <TextField name="txtId" row="0" column="1" />
          <TextField name="txtName" row="1" column="1" />
          <TextField name="txtAddress" row="2" column="1" />
          <TextField name="txtCity" row="3" column="1" />
          <TextField name="txtCountry" row="4" column="1" />
        </Grid>
      </Composite>
    </View>
  </Application>
</Root>

Here's the Java code to retrieve the required node list:

try {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    domFactory.setIgnoringComments(true);
    domFactory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse("D:\TEST\myFile.xml");

    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nl = (NodeList) xpath.evaluate("//Model//DataTable", dDoc, XPathConstants.NODESET);
    System.out.println(nl.getLength());

}catch (Exception ex) {
    System.out.println(ex.getMessage());
}

There is no problem loading and parsing the XML file, and I see the correct nodes in dDoc. The problem is that xpath returns nothing when evaluating my expression. I tried many other expressions for testing purposes, but every time, as a result, the NodeList "nl" has no element

+3
source share
1 answer

, XML , .

XPath <DataTable> XML-.

+2

Source: https://habr.com/ru/post/1748680/


All Articles