使用Xpath为属性添加新值

时间:2015-04-29 17:04:55

标签: java xml xpath

我需要使用xpath通过添加新值来导航到分析/分析参数属性:

作为第一步,我尝试从分析标记中检索一个值,但无法使其工作(没有检索样本值,控制台中没有输出)。任何人都可以在这里看到我出错的地方,然后进一步说明如何添加新值。

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

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

1 个答案:

答案 0 :(得分:1)

目前还不是很清楚你的最终目标是什么,但是

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

选择settings元素,因为它具有属性@analysis谓词(尖括号内)的含义。如果这不是您想要的,请使用

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

选择@analysis元素的settings属性。

不熟悉Java,但我还要做两个猜测:

  • 输入文档中是否有名称空间,但您没有显示?
  • 或许这不是处理属性节点的正确方法。试试

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

编辑:现在测试了Java代码,

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

肯定有效,在纠正当前格式不正确的输入XML 后,viewpoint元素未关闭。

我得到了输出:

$ java XPathTestReports
someValue