使用Dom4J解析XML(二)

3.生成xml文件

package domj4Parser;   
  
import java.util.List;   
import java.util.Iterator;   
import java.io.*;   
//导入 dom4j API 类   
import org.dom4j.Document;   
import org.dom4j.Element;   
import org.dom4j.Attribute;   
import org.dom4j.Node;   
import org.dom4j.io.OutputFormat;   
import org.dom4j.io.XMLWriter;   
import org.dom4j.DocumentException;   
import org.dom4j.io.SAXReader;   
  
public class Dom4Jparser{   
    public void modifyDocument(File inputXml){   
        try{   
            //使用 SAXReader 解析 XML 文档 catalog.xml   
            SAXReader saxReader = new SAXReader();   
            //inputXml 是从 catalog.xml 创建的 java.io.File   
            Document document = saxReader.read(inputXml);   
               
            //使用 XPath 表达式从 article 元素中获得 level 节点列表   
            List list = document.selectNodes("//article/@level");   
            Iterator iter=list.iterator();   
            while(iter.hasNext()){   
                //如果 level 属性值是"Intermediate"则改为"Introductory"   
                Attribute attribute=(Attribute)iter.next();   
                if(attribute.getValue().equals("Intermediate"))   
                    attribute.setValue("Introductory");   
            }   
               
            list = document.selectNodes("//article/@date" );   
               
            iter=list.iterator();   
            while(iter.hasNext()){   
                Attribute attribute=(Attribute)iter.next();   
                if(attribute.getValue().equals("December-2001"))   
                    attribute.setValue("October-2002");   
            }   
               
            //获取 article 元素列表   
            list = document.selectNodes("//article" );   
            iter=list.iterator();   
            while(iter.hasNext()){   
                Element element=(Element)iter.next();   
                //使用Visitor模式遍历子节点   
                element.accept(new MyVisitor());   
                //从 article 元素中的 title 元素得到一个迭代器   
                Iterator iterator=element.elementIterator("title");   
                while(iterator.hasNext()){   
                    Element titleElement=(Element)iterator.next();   
                    //并修改 title 元素的文本   
                    if(titleElement.getText().equals("Java configuration with XMLSchema"))   
                        titleElement.setText("Create flexible and extensible XML schema");   
                }   
            }   
            list = document.selectNodes("//article/author" );   
            iter=list.iterator();   
            while(iter.hasNext()){   
                Element element=(Element)iter.next();   
                Iterator iterator=element.elementIterator("firstname");   
                while(iterator.hasNext()){   
                    Element firstNameElement=(Element)iterator.next();   
                    if(firstNameElement.getText().equals("Marcello"))   
                        firstNameElement.setText("Ayesha");   
                }   
            }   
            list = document.selectNodes("//article/author" );   
            iter=list.iterator();   
            while(iter.hasNext()){   
                Element element=(Element)iter.next();   
                Iterator iterator=element.elementIterator("lastname");   
                while(iterator.hasNext()){   
                    Element lastNameElement=(Element)iterator.next();   
                    if(lastNameElement.getText().equals("Vitaletti"))   
                        lastNameElement.setText("Malik");   
                }   
            }   
            XMLWriter output = new XMLWriter(   
                    new FileWriter( new File("xmlFile/catalog-modified.xml")));   
            output.write(document);   
            output.close();   
        }   
        catch(DocumentException e){   
            e.printStackTrace();   
        }   
        catch(IOException e){   
            System.out.println(e.getMessage());   
        }   
    }   
    public void treeWalk(Element element) {   
        for (int i = 0,size = element.nodeCount();i < size;i++){   
            Node node = element.node(i);   
            if (node instanceof Element) {   
                treeWalk((Element) node);   
            }    
            else { /* do something....*/}   
        }   
    }   
    //处理中文问题   
    public int formatXMLFile(String filename){   
        int returnValue = 0;   
        try{   
           SAXReader saxReader = new SAXReader();       
           Document document = saxReader.read(new File(filename));   
           XMLWriter output = null;   
           /** 格式化输出,类型IE浏览一样 */  
           OutputFormat format = OutputFormat.createPrettyPrint();   
           /** 指定XML字符集编码 */  
           format.setEncoding("GBK");   
           output = new XMLWriter(new FileWriter(new File(filename)),format);   
           output.write(document);   
           output.close();           
           /** 执行成功,需返回1 */  
           returnValue = 1;          
        }   
        catch(Exception ex){   
            ex.printStackTrace();   
        }   
        return returnValue;   
    }   
  
    public static void main(String[] argv){   
        Dom4Jparser dom4jParser=new Dom4Jparser();   
        dom4jParser.modifyDocument(new File("xmlFile/catalog.xml"));   
        dom4jParser.formatXMLFile("xmlFile/catalog.xml");   
    }   
}  
4.使用Visitor模式

package domj4Parser;   
  
import org.dom4j.Attribute;   
import org.dom4j.Element;   
import org.dom4j.VisitorSupport;   
  
//Visitor模式   
public class MyVisitor extends VisitorSupport {   
    public void visit(Element element){   
         System.out.println(element.getName());   
    }   
    public void visit(Attribute attr){   
         System.out.println(attr.getName());   
    }   
}  

 

你可能感兴趣的:(xml,浏览器,IE)