如何在XML中查找和替换属性值 [英] How to find and replace an attribute value in a XML

查看:203
本文介绍了如何在XML中查找和替换属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java构建一个 XML扫描器,以查找以!Here:开头的属性值。该属性值包含稍后替换的指令。例如,以
为例,我在这个xml文件中填充了诸如

I am building a "XML scanner" in Java that finds attribute values starting with "!Here:". The attribute value contains instructions to replace later. for example I have this xml file filled with records like

<bean value="!Here:Sring:HashKey"></bean>

如何仅在以开头的情况下查找和替换属性值 !Here:

推荐答案

为了修改XML文件中的某些元素或属性值,但仍要尊重XML结构,但您需要使用XML解析器。不仅仅是 String $ replace() ...

In order to modify some element or attribute values in the XML file, while still being respectful of XML structure, you will need to use a XML parser. It's a bit more involved than just String$replace()...

给出一个示例XML,例如: / p>

Given an example XML like:

<?xml version="1.0" encoding="UTF-8"?>
<beans> 
    <bean id="exampleBean" class="examples.ExampleBean">
        <!-- setter injection using -->
        <property name="beanTwo" ref="anotherBean"/>
        <property name="integerProperty" value="!Here:Integer:Foo"/>
    </bean>
    <bean id="anotherBean" class="examples.AnotherBean">
        <property name="stringProperty" value="!Here:String:Bar"/>
    </bean>
</beans>

为了更改2个标记,在这里,则需要

In order to change the 2 markers !Here, you need


  1. 将文件加载到dom Document 中,

  2. 使用xpath选择所需的节点。在这里,我用属性 value 搜索文档中的所有节点,该属性包含字符串!Here 。 xpath表达式为 // * [包含(@value,'!Here')]

  3. 执行转换您想要在每个选定节点上。在这里,我只是将!这里的更改为什么?

  1. to load the file into a dom Document,
  2. select with xpath the wanted nodes. Here I search for all nodes in the document with an attribute value that contains the string !Here. The xpath expression is //*[contains(@value, '!Here')].
  3. do the transformation you want on each selected nodes. Here I just change !Here by What?.

将修改后的dom 文档保存到新文件中。

save the modified dom Document into a new file.







static String inputFile = "./beans.xml";
static String outputFile = "./beans_new.xml";

// 1- Build the doc from the XML file
Document doc = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder().parse(new InputSource(inputFile));

// 2- Locate the node(s) with xpath
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xpath.evaluate("//*[contains(@value, '!Here')]",
                                          doc, XPathConstants.NODESET);

// 3- Make the change on the selected nodes
for (int idx = 0; idx < nodes.getLength(); idx++) {
    Node value = nodes.item(idx).getAttributes().getNamedItem("value");
    String val = value.getNodeValue();
    value.setNodeValue(val.replaceAll("!Here", "What?"));
}

// 4- Save the result to a new XML doc
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));

生成的XML文件为:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans> 
    <bean class="examples.ExampleBean" id="exampleBean">
        <!-- setter injection using -->
        <property name="beanTwo" ref="anotherBean"/>
        <property name="integerProperty" value="What?:Integer:Foo"/>
    </bean>
    <bean class="examples.AnotherBean" id="anotherBean">
        <property name="stringProperty" value="What?:String:Bar"/>
    </bean>
</beans>

这篇关于如何在XML中查找和替换属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆