<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy />
<bean id="A" class="A" scope="singleton">
<constructor-arg index='0'><ref bean='B'/></constructor-arg>
</bean>
<bean id="B" class="B" scope="singleton">
<aop:scoped-proxy proxy-target-class="true"/>
<constructor-arg index="0"><ref bean="B" /></constructor-arg>
</bean>
<bean id="C" class="C" scope="singleton">
<aop:scoped-proxy proxy-target-class="true"/>
<constructor-arg index="0"><ref bean="C" /></constructor-arg>
</bean>
</beans>
我有一个与此类似的 xml。我正在尝试解析这个 xml 并查找是否有一个名为 scoped-proxy 的元素,如果有我想删除整个 bean 标记。我想为所有实例做。
例如 :
对于 bean id :"B" ,它有这个元素,所以我想删除整个
<bean id="B" class="B" scope="singleton">
<aop:scoped-proxy proxy-target-class="true"/>
<constructor-arg index="0"><ref bean="B" /></constructor-arg>
</bean>
删除后我想写一个新的xml文件。
示例代码:
import java.util.HashSet;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Node;
public class XMLParser {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource("src/a.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
HashSet<String> idSet = new HashSet<>();
while(xsr.hasNext()) {
if(xsr.isStartElement() && "bean".equals(xsr.getLocalName())) {
String value=xsr.getAttributeValue(1);
xsr.nextTag();
if("scoped-proxy".equals(xsr.getLocalName())){
idSet.add(value);
System.out.println(value);
}
}
xsr.next();
}
}
}
由于我是使用 java 解析 xml 的新手,请告诉我如何在找到并写入新文件时删除完整元素