【问题标题】:Xml parsing using xmlstream Reader and writing into a new xml file使用 xmlstream Reader 解析 XML 并写入新的 xml 文件
【发布时间】:2018-03-05 22:36:57
【问题描述】:
 <?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 的新手,请告诉我如何在找到时删除完整元素并写入新文件

【问题讨论】:

    标签: java xml xmlstreamreader


    【解决方案1】:

    一个相当直接的解决方案是将整个源文档读入内存,然后手动操作 DOM:

    public class DomFilter {
        public static void main(String[] args) throws Exception {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder parser = factory.newDocumentBuilder();
    
            try (InputStream in = new FileInputStream("src/a.xml")) {
                // Parse the source
                Document doc = parser.parse(in);
    
                // Find all "bean" elements
                NodeList beans = doc.getElementsByTagNameNS("http://www.springframework.org/schema/beans", "bean");
                List<Element> toRemove = new ArrayList<>();
                for (int i = 0; i < beans.getLength(); i++) {
                    Element bean = (Element) beans.item(i);
                    // Does the bean element have a "scoped-proxy" child element?
                    if (bean.getElementsByTagNameNS("http://www.springframework.org/schema/aop", "scoped-proxy").getLength() > 0) {
                        // Yes... add it to the remove list
                        toRemove.add(bean);
                    }
                }
    
                // Remove the elements we found
                toRemove.forEach(e -> e.getParentNode().removeChild(e));
    
                // Write the tree out using an identity transformer
                Transformer writer = TransformerFactory.newInstance().newTransformer();
                writer.transform(new DOMSource(doc), new StreamResult(System.out));
            }
        }
    }
    

    【讨论】:

    • 非常感谢您的帮助。您的解决方案很有帮助。
    • 你能看看我的解决方案并告诉我哪里出错了。非常感谢您的帮助。
    【解决方案2】:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    
    public class DomFilter {
        public static void main(String[] args) throws Exception {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder parser = factory.newDocumentBuilder();
    
            try (InputStream in = new FileInputStream("src/a.xml"))
            {
                // Parse the source
                Document doc = parser.parse(in);
    
                String filepath = "src/filtered.xml";
                StreamResult result = new StreamResult(new File(filepath));
    
                // Find all "bean" elements
                NodeList beans = doc.getElementsByTagNameNS("http://www.springframework.org/schema/beans", "bean");
                List<Element> toRemove = new ArrayList<>();
                for (int i = 0; i < beans.getLength(); i++) {
                    Element bean = (Element) beans.item(i);
                    // Does the bean element have a "scoped-proxy" child element?
                    if (bean.getElementsByTagNameNS("http://www.springframework.org/schema/aop", "scoped-proxy").getLength() > 0) {
                        // Yes... add it to the remove list
                        toRemove.add(bean);
                    }
                }
    
                // Remove the elements we found
                toRemove.forEach(e -> e.getParentNode().removeChild(e));
    
                // Write the tree out using an identity transformer
                Transformer writer = TransformerFactory.newInstance().newTransformer();
                writer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                //writer.setOutputProperty(OutputKeys.INDENT, "yes");
                writer.transform(new DOMSource(doc), result);
            }
        }
    }
    

    【讨论】:

    • 看起来不错,但您应该在 try-with-resources 块中初始化输出流,以确保它在退出时被刷新并关闭。
    猜你喜欢
    • 2015-05-24
    • 1970-01-01
    • 2021-12-07
    • 2015-05-14
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    相关资源
    最近更新 更多