HOME       LIST

【JAVA】XML文書操作サンプル(生成、追加、変更、削除)

XML文書を生成、ノード追加、ノードの属性値変更、ノード削除のサンプルコード

どのような状況に置かれても肯定的な思考をすること。 - 2017/03/18(DOJEUN)

I think she's nice.(いい人だと思う。)


1.XML文書生成(CreateXML.java)

/*
 XMLファイル生成(test01.xml)
*/
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;

public class CreateXML{
  public static void main(String[] args)
  {
    DocumentBuilder documentBuilder = null;
    try
    {
       documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e)
    {
       e.printStackTrace();
    }

    Document document = documentBuilder.newDocument();
    Element root = document.createElement("root");
    document.appendChild(root);

    File file = new File("test01.xml");
    createxml(file, document);
  }

  public static boolean createxml(File file, Document document){
    Transformer transformer = null;
    try
    {
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e)
    {
      e.printStackTrace();
      return false;
    }
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty("encoding", "UTF-8");

    try
    {
      transformer.transform(new DOMSource(document), new StreamResult(file));
    } catch (TransformerException e)
    {
      e.printStackTrace();
      return false;
    }

    return true;
  }
}

実行結果

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root/>

2.ノードの追加(AddXML.java)

/*
 ノード追加
 test ノードを3つ追加
*/
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;

public class AddXML{
  public static void main(String[] args)
  {
    try
    {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = dbf.newDocumentBuilder();
      File file = new File("test01.xml");
      Document document = builder.parse(file);
      Element root = document.getDocumentElement();

      Element item = document.createElement("test");
      item.setAttribute("sn","1");
      item.appendChild(document.createTextNode("no 1"));
      root.appendChild(item);

      Element item2 = document.createElement("test");
      item2.setAttribute("sn","2");
      item2.appendChild(document.createTextNode("no 2"));
      root.appendChild(item2);

      Element item3 = document.createElement("test");
      item3.setAttribute("sn","3");
      item3.appendChild(document.createTextNode("no 3"));
      root.appendChild(item3);

      createxml(file, document);

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

  public static boolean createxml(File file, Document document){
    Transformer transformer = null;
    try
    {
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e)
    {
      e.printStackTrace();
      return false;
    }
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty("encoding", "UTF-8");

    try
    {
      transformer.transform(new DOMSource(document), new StreamResult(file));
    } catch (TransformerException e)
    {
      e.printStackTrace();
      return false;
    }

    return true;
  }
}

実行結果

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<test sn="1">no 1< /test>
<test sn="2">no 2< /test>
<test sn="3">no 3< /test>
</root>

3.ノードの属性値変更(ModXML.java)

/*
 ノード検索ー>属性値変更
 属性 sn=2を sn=20001に変更
*/
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import org.w3c.dom.NodeList;

public class ModXML{
  private static final String sSearch = "2";

  public static void main(String[] args)
  {
    try
    {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = dbf.newDocumentBuilder();
      File file = new File("test01.xml");
      Document document = builder.parse(file);

      Element root = document.getDocumentElement();

      NodeList list = root.getElementsByTagName("test");
      for (int i=0; i < list.getLength() ; i++)
      {
         Element element = (Element)list.item(i);
         String strSN = element.getAttribute("sn");
         if(strSN.compareTo(sSearch) == 0)
         {
           element.setAttribute("sn", "20001");
           break;
         }
      }

      createxml(file, document);

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

  public static boolean createxml(File file, Document document){
    Transformer transformer = null;
    try
    {
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e)
    {
      e.printStackTrace();
      return false;
    }
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty("encoding", "UTF-8");

    try
    {
      transformer.transform(new DOMSource(document), new StreamResult(file));
    } catch (TransformerException e)
    {
      e.printStackTrace();
      return false;
    }

    return true;
  }
}

実行結果

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<test sn="1">no 1< /test>
<test sn="20001">no 2< /test>
<test sn="3">no 3< /test>
</root>

4.ノード削除

/*
 ノード検索ー>ノード削除
 属性 sn="20001"ノードを削除する。
*/
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import org.w3c.dom.NodeList;

public class DelXML{
  private static final String sSearch = "20001";

  public static void main(String[] args)
  {

    boolean wrkChk = false;
    int j = 0;

    try
    {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = dbf.newDocumentBuilder();
      File file = new File("test01.xml");
      Document document = builder.parse(file);

      Element root = document.getDocumentElement();

      NodeList list = root.getElementsByTagName("test");
      for (int i=0; i < list.getLength() ; i++)
      {
         Element element = (Element)list.item(i);
         String strSN = element.getAttribute("sn");
         if(strSN.compareTo(sSearch) == 0)
         {
           wrkChk = true;
           j = i;
           break;
         }
      }

      if(wrkChk == true)
      {
        root.removeChild(list.item(j));
      }

      createxml(file, document);

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

  public static boolean createxml(File file, Document document){
    Transformer transformer = null;
    try
    {
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e)
    {
      e.printStackTrace();
      return false;
    }
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty("encoding", "UTF-8");

    try
    {
      transformer.transform(new DOMSource(document), new StreamResult(file));
    } catch (TransformerException e)
    {
      e.printStackTrace();
      return false;
    }

    return true;
  }
}

実行結果

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<test sn="1">no 1< /test>

<test sn="3">no 3< /test>
</root>

以前コンテンツ:【JAVA】データタイプメモ
次のコンテンツ:【JAVA】PrintWriterを利用してテキストを書き込むサンプル



Copyright(c) 2007-2020 dojeun.com All Rights Reserved.

Valid XHTML 1.0 Transitional