心远专栏

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  24 随笔 :: 0 文章 :: 9 评论 :: 0 Trackbacks

1.生成一个XML文件:

public   void  generateDocument()  {
        
/**
         * 使用 DocumentHelper 类创建一个文档实例。 
         * DocumentHelper 是生成 XML 文档节点的 dom4j API 工厂类。
         
*/

        Document document 
=  DocumentHelper.createDocument();
        
/**
         * 使用 addElement() 方法创建根元素 catalog 。 
         * addElement() 用于向 XML 文档中增加元素。
         
*/

        Element catalogElement 
=  document.addElement( " catalog " );
        
/**
         * 在 catalog 元素中使用 addComment() 方法添加注释“An XML catalog”。 
         
*/

        catalogElement.addComment(
" An XML catalog " );
        
/**
         * 在 catalog 元素中使用 addElement() 方法增加 journal 元素。
         
*/

        Element journalElement 
=  catalogElement.addElement( " journal " );
        
/**
         * 使用 addAttribute() 方法向 journal 元素添加 title 和 publisher 属性。
         
*/

        journalElement.addAttribute(
" title " " XML Zone " );
        journalElement.addAttribute(
" publisher " " IBM developerWorks " );

        
/**
         * 向 article 元素中添加 journal 元素。 
         
*/

        Element articleElement 
=  journalElement.addElement( " article " );
        
/**
         * 为 article 元素增加 level 和 date 属性。 
         
*/

        articleElement.addAttribute(
" level " " Intermediate " );
        articleElement.addAttribute(
" date " " December-2001 " );
        
/**
         * 向 article 元素中增加 title 元素。 
         
*/

        Element titleElement 
=  articleElement.addElement( " title " );
        
/**
         * 使用 setText() 方法设置 article 元素的文本。 
         
*/

        titleElement.setText(
" Java configuration with XML Schema " );
        
/**
         * 在 article 元素中增加 author 元素。 
         
*/

        Element authorElement 
=  articleElement.addElement( " author " );
        
/**
         * 在 author 元素中增加 firstname 元素并设置该元素的文本。 
         
*/

        Element firstNameElement 
=  authorElement.addElement( " firstname " );
        firstNameElement.setText(
" Macello " );
        
/**
         * 在 author 元素中增加 lastname 元素并设置该元素的文本。 
         
*/

        Element lastNameElement 
=  authorElement.addElement( " lastname " );
        lastNameElement.setText(
" Vitaletti " );

        
try   {
            XMLWriter output 
=   new  XMLWriter( new  FileWriter( new  File(
                    
" demo/catalog.xml " )));
            output.write(document);
            output.close();
        }
  catch  (IOException e)  {
            e.printStackTrace();
        }

}

生成的XML文件如下所示:

<? xml version="1.0" encoding="UTF-8" ?>
< catalog >
<!-- An XML catalog -->
    
< journal  title ="XML Zone"  publisher ="IBM developerWorks" >
        
< article  level ="Intermediate"  date ="December-2001" >
            
< title > Java configuration with XML Schema </ title >
            
< author >
                
< firstname > Macello </ firstname >
                
< lastname > Vitaletti </ lastname >
            
</ author >
        
</ article >
    
</ journal >
</ catalog >


2.读取该文件,输出firstname和lastname:

public   void  outPutInfo(String filename) {
        
try  
            File f 
=   new  File(filename); 
            SAXReader reader 
=   new  SAXReader(); 
            Document doc 
=  reader.read(f); 
            
            List list 
=  doc.selectNodes( " //article " );
            Iterator iter 
=  list.iterator();
            
            
while  (iter.hasNext())  {
                Element element 
=  (Element) iter.next();
                Element nameElement 
=   null ;
                Iterator iterator 
=  element.elementIterator( " author " );
                
while  (iterator.hasNext())  {
                    nameElement 
=  (Element) iterator.next();
                    System.out.println(nameElement.elementText(
" firstname " ));
                    System.out.println(nameElement.elementText(
" lastname " ));
                }

            }

        }
  catch  (Exception e) 
            e.printStackTrace(); 
        }
 
    }


3.修改该文件;

public   void  modifyDocument(String filename)  {
        
try   {
            File inputXml 
=   new  File(filename);
            
            SAXReader saxReader 
=   new  SAXReader();
            Document document 
=  saxReader.read(inputXml);

            
/**
             * inputXml 是从 catalog.xml 创建的 java.io.File。
             * 使用 XPath 表达式从 article 元素中获得 level 节点列表。
             * 如果 level 属性值是“Intermediate”则改为“Introductory”。 
             
*/

            List list 
=  document.selectNodes( " //article/@level " );
            Iterator iter 
=  list.iterator();
            
while  (iter.hasNext())  {
                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 元素列表,从 article 元素中的 title 元素得到一个迭代器,并修改 title 元素的文本。 
             
*/

            list 
=  document.selectNodes( " //article " );
            iter 
=  list.iterator();
            
while  (iter.hasNext())  {
                Element element 
=  (Element) iter.next();
                Iterator iterator 
=  element.elementIterator( " title " );
                
while  (iterator.hasNext())  {
                    Element titleElement 
=  (Element) iterator.next();
                    
if  (titleElement.getText().equals(
                            
" Java configuration with XML Schema " ))
                        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(
                    
" catalog-modified.xml " )));
            output.write(document);
            output.close();
        }
  catch  (DocumentException e)  {
            System.out.println(e.getMessage());
        }
  catch  (IOException e)  {
            System.out.println(e.getMessage());
        }

    }
更新后的XML文件内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!--An XML catalog-->
    
<journal title="XML Zone" publisher="IBM developerWorks">
        
<article level="Introductory" date="October-2002">
            
<title>Create flexible and extensible XML Schema</title>
            
<author>
                
<firstname>Macello</firstname>
                
<lastname>Malik</lastname>
            
</author>
        
</article>
    
</journal>
</catalog>

posted on 2006-11-13 12:48 心远 阅读(166) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: