Kelv
Kelv

Reputation: 49

Using Xpath to add a new value to attributes

I need to use xpath to navigate to the analysis/analysis parameter attributes by add in a new value:

As a first step I have tried retrieving a value from the analysis tag but cannot get this to work (no sample value being retrieved, no output in console). Can anyone see where I am going wrong here and then further show how I can then add a new value.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XPathTestReports {
    public static void main(String[] args) {


        try {


                 String xpath = "/UserDocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings[@analysis]";

                 FileInputStream file = new FileInputStream(new File("c:/workspace/savedreportscatalog.xml"));

                 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

                 DocumentBuilder builder = builderFactory.newDocumentBuilder();

                 Document xmlDocument = builder.parse(file);

                 XPath xPath = XPathFactory.newInstance().newXPath();
                 XPathExpression xPathExpression = xPath.compile(xpath);
                 String attributeValue = "" + xPathExpression.evaluate(xmlDocument, XPathConstants.STRING);
                 System.out.println(attributeValue);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }       



    }
 }

XML Sample

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<UserDocument>
  <report-plan-catalog>
    <collection timestamp="" title="report plans" uid="">
      <collection container-id="" timestamp="2015-04-29" title="*" uid="">
        <collection container-id="94533" timestamp="2015-04-29" title="*" uid="5cfc">
          <report-config container-id="5cfc" timestamp="2015-04-29" title="Asset" type="Risk" uid="4718">
            <configuration>
              <reportType>Live</reportType>
            </configuration>
            <report-plan name="Asset">
              <columns>
                <column name="Nom" subtotal-function="Sum" total-function="Sum"/>
                <column name="Id"/>
                <column name="Ref"/>
              </columns>
              <settings analysis="someValue" analysisParameters="" filtering-enabled="true" object-actions="false" show-object-actions="true" sorting-enabled="true"/>
              <viewpoint kind="simple">
                <slices/>
         </report-plan>
      </report-config>
    </collection>
  </collection>
</collection>
</report-plan-catalog>
</UserDocument>

Upvotes: 0

Views: 2285

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22647

It's still not very clear what your end goal is, but

/UserDocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings[@analysis]

selects a settings element, given that is has an attribute @analysis, that's what the predicate (inside angle brackets) means. If that is not what you want, use

/UserDocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings/@analysis

to select the @analysis attribute of the settings element.


Not very familiar with Java, but I'll make two further guesses:

  • is there perhaps a namespace in your input document that you have not shown?
  • perhaps this is not the right way to deal with attribute nodes. Try

    string(/UserDocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings/@analysis)


EDIT: Now tested the Java code, and

String xpath = "/UserDocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings/@analysis";

definitely works, after correcting the input XML, which is currently not well-formed - the viewpoint element is not closed.

I get as output:

$ java XPathTestReports
someValue

Upvotes: 1

Related Questions