/* 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/>
/* ノード追加 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>
/* ノード検索ー>属性値変更 属性 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>
/* ノード検索ー>ノード削除 属性 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>