Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/users/2/moriya-h5/web/confrage/java/xml/write_dom4j/write_dom4j.html on line 2

Warning: include_once(http://www.confrage.com/bin/header.php): failed to open stream: no suitable wrapper could be found in /home/users/2/moriya-h5/web/confrage/java/xml/write_dom4j/write_dom4j.html on line 2

Warning: include_once(): Failed opening 'http://www.confrage.com/bin/header.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/xml/write_dom4j/write_dom4j.html on line 2
dom4jでxmlを書き込む | Java | 株式会社CONFRAGE
Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/xml/write_dom4j/write_dom4j.html on line 17

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/xml/write_dom4j/write_dom4j.html on line 17
dom4jでxmlを書き込む
dom4jでxmlを書き込むには、SAXReader#readメソッドでxmlファイルを読み込みます。
戻り値はDocumentクラスです。このクラスでxmlファイルを操作することが可能です。

selectNodesとelementIterator
Document#selectNodesメソッドでxmlのノードを取得できます。以下のようなxmlファイルがあるとします。

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!--An XML Catalog-->
<?target instruction?>
  <journal title="XML Zone"
                  publisher="IBM developerWorks">

<article level="Intermediate" date="December-2001">
 <title>Java configuration with XML Schema</title>
 <author>
     <firstname>Marcello</firstname>
     <lastname>Vitaletti</lastname>
 </author>
  </article>
  </journal>
</catalog>
articleというノードのlevel属性を取得したい場合はselectNodes("//article/@level")とします。
titleタグで囲まれた値を取得したい場合はElement#elementIterator("title")とします。

サンプル
package jp.co.confrage;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Test005 {

    public static void main(String[] args) {
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(new File("C:\\a001.xml"));
            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("test"); // testに書き換える
                }
            }

            list.clear();
            list = document.selectNodes("//article/author");
            Iterator iter2=list.iterator();
            while(iter2.hasNext()){
                Element element=(Element)iter2.next();
                Iterator iterator=element.elementIterator("firstname");
                while(iterator.hasNext()){
                    Element firstNameElement=(Element)iterator.next();
                    if(firstNameElement.getText().equals("Marcello")){
                        System.out.println(firstNameElement.getText());
                        firstNameElement.setText("test"); //testに書き換える
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

Back to top

Information

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/xml/write_dom4j/write_dom4j.html on line 145

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/xml/write_dom4j/write_dom4j.html on line 145

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/xml/write_dom4j/write_dom4j.html on line 154

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/xml/write_dom4j/write_dom4j.html on line 154