【问题标题】:Change inside value of XML specific tag更改 XML 特定标签的内部值
【发布时间】:2016-01-18 15:21:35
【问题描述】:

想象一下这个 XML:

<DocumentElement>
  <PropsAndValues>
    <Name># CONFIGURATION_NODE # DO NOT DELETE THIS ROW #</Name>
    <Keywords>#</Keywords>
    <Tests>#</Tests>
    <Type>#</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>PersonSuiteTC-1</Name>
    <Keywords/>
    <Tests>Definition:"Business Process":PersonSuiteTC-1</Tests>
    <Type>HIGH</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>No Operation</Name>
    <Keywords/>
    <Tests/>
    <Type>TECH</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>Start</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>Open Application</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>Go To Profile Finder</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>Search Person</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>End</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>PersonSuiteTC-1</Name>
    <Keywords>
    Start Open Application Go To Profile Finder Search Person End
    </Keywords>
    <Tests/>
    <Type>HIGH</Type>
  </PropsAndValues>
</DocumentElement>

我需要用这个头创建一个方法:

private static void addRelation(String kWName, String elemName) throws Exception {

}

将elemName 添加到具有&lt;Name&gt; 标签值kwName 的节点的标签&lt;Keywords&gt;

例如:

addRelation("PersonSuiteTC-1", "Add this String to &lt;Keywords&gt; tag"),应该转到名称为“PersonSuiteTC-1”的节点,并将“将此字符串添加到标签”添加到&lt;Keywords&gt; 标签。

最简单的方法是什么?

谢谢。

【问题讨论】:

  • 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决该问题所做的工作。

标签: java xml xpath methods tags


【解决方案1】:

1 解析您的 XML。

例如:

String xml= your xml
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));

2 找到您的插入点。

使用 XPath,类似这样的东西:

XPath xpath = XPathFactory.newInstance().newXPath();
expression="//Name[text()='PersonSuiteTC-1']";

XPathExpression expr = xpath.compile(expression) ; 
NodeList nodes  = (NodeList) expr.evaluate(document, XPathConstants.NODESET);

请注意,此文本有多个节点

你可以迭代:

for (int k = 0; k

  Node nodeSegment = nodes.item(k);

3 插入您的数据:

使用createElement()、createTextNode()、appendChild()

看到:How do I append a node to an existing XML file in java

4 生成你的xml返回:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result out = new StreamResult(new File("result.xml"));
Source in = new DOMSource(document);
transformer.transform(in, out); 

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2020-12-20
    • 2020-10-20
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2017-05-23
    • 2023-03-04
    相关资源
    最近更新 更多