如何在XML中查找和替换属性值 [英] How to find and replace an attribute value in a 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
- 将文件加载到dom
Document
中, - 使用xpath选择所需的节点。在这里,我用属性
value
搜索文档中的所有节点,该属性包含字符串!Here
。 xpath表达式为// * [包含(@value,'!Here')]
。 -
执行转换您想要在每个选定节点上。在这里,我只是将
!这里的
更改为什么?
。
- to load the file into a dom
Document
, - 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')]
. do the transformation you want on each selected nodes. Here I just change
!Here
byWhat?
.
将修改后的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屋!