javaはDOMを使ってXMLドキュメントを添削して操作例コードを調べます。


本論文の研究は主にjavaがDOMを使用してXMLドキュメントを添削して調べています。具体的な例は以下の通りです。
ソースコード:

package com.zc.homeWork18;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLWriter {

  private static String xmlPath = "src\\com\\zc\\homeWork18\\MyXml.xml";

  public static void getFamilyMemebers() {

  /*
     *         
     */
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    //            XML               ,  true,   false
    dbf.setIgnoringElementContentWhitespace(true);

    try {
      /*
       *       
       */
      DocumentBuilder db = dbf.newDocumentBuilder();//      ,  XML  
      Document doc = db.parse(xmlPath); //   dom  xml  

      /*
       *     ,  XML       
       */
      //                 
      NodeList sonlist = doc.getElementsByTagName("son");
      for (int i = 0; i < sonlist.getLength(); i++) //       
      {
        //        
        Element son = (Element) sonlist.item(i);
        //     son       
        for (Node node = son.getFirstChild(); node != null; node = node
            .getNextSibling()) {
          //          
          if (node.getNodeType() == Node.ELEMENT_NODE) {
            String name = node.getNodeName();
            String value = node.getFirstChild().getNodeValue();
            System.out.println(name + " : " + value);
          }
        }
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }

  //   
  public static void modifySon() {
    //         
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    try {
      //  XML     DOM    
      DocumentBuilder db = dbf.newDocumentBuilder();
      //   Document  
      Document xmldoc = db.parse(xmlPath);

      //      
      Element root = xmldoc.getDocumentElement();
      //   id 001   
      Element per = (Element) selectSingleNode("/father/son[@id='001']",
          root);
      //  age        28
      per.getElementsByTagName("age").item(0).setTextContent("28");
      //   
      TransformerFactory factory = TransformerFactory.newInstance();
      Transformer former = factory.newTransformer();
      former.transform(new DOMSource(xmldoc), new StreamResult(new File(
          xmlPath)));
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }

  //       ,    ,    
  public static void discardSon() {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);

    try {

      DocumentBuilder db = dbf.newDocumentBuilder();
      Document xmldoc = db.parse(xmlPath);
      //      
      Element root = xmldoc.getDocumentElement();
      //        id=002   
      Element son = (Element) selectSingleNode("/father/son[@id='002']",
          root);
      //      
      root.removeChild(son);
      //   
      TransformerFactory factory = TransformerFactory.newInstance();
      Transformer former = factory.newTransformer();
      former.transform(new DOMSource(xmldoc), new StreamResult(new File(
          xmlPath)));

    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }

  //     
  public static void createSon() {
    //         
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(false);

    try {

      DocumentBuilder db = dbf.newDocumentBuilder();
      //   Document  
      Document xmldoc = db.parse(xmlPath);
      //      
      Element root = xmldoc.getDocumentElement();
      //     son,     id 004
      Element son = xmldoc.createElement("son");
      son.setAttribute("id", "004");
      //     name
      Element name = xmldoc.createElement("name");
      name.setTextContent("   ");
      son.appendChild(name);
      //     age
      Element age = xmldoc.createElement("age");
      age.setTextContent("0");
      son.appendChild(age);
      //  son       
      root.appendChild(son);
      //   
      TransformerFactory factory = TransformerFactory.newInstance();
      Transformer former = factory.newTransformer();
      former.transform(new DOMSource(xmldoc), new StreamResult(new File(
          xmlPath)));

    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }

  //       
  public static Node selectSingleNode(String express, Element source) {
    Node result = null;
    //  XPath  
    XPathFactory xpathFactory = XPathFactory.newInstance();
    //  XPath  
    XPath xpath = xpathFactory.newXPath();
    try {
      result = (Node) xpath.evaluate(express, source, XPathConstants.NODE);
      System.out.println(result);
    } catch (XPathExpressionException e) {
      System.out.println(e.getMessage());
    }

    return result;
  }

  //   
  public static void main(String[] args) {

    getFamilyMemebers();
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    modifySon();
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("    ");
    getFamilyMemebers();
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    discardSon();
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("    ");
    getFamilyMemebers();
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    createSon();
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("    ");
    getFamilyMemebers();
  }
}
XMLファイル

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<father>
  <son id="001">
    <name>  </name>
    <age>20</age>
  </son>
  <son id="002">
    <name>  </name>
    <age>18</age>
  </son>
  <son id="003">
    <name>  </name>
    <age>13</age>
  </son>

</father>
締め括りをつける
以上は、JavaがDOMを使ってXMLドキュメントを添削して調べている実例コードの全部の内容についてです。興味のある方は引き続き当駅の他のテーマを参照してください。友達のサポートに感謝します。