0
 <?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を解析するのが初めてなので、要素が見つかったときに完全な要素を削除して新しいファイルに書き込む方法を教えてください

4

2 に答える 2