サンプルプログラムを次に示します。
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.xml.sax.SAXException;
import org.w3c.dom.*;
public class SampleDOM
{
public static final void main(String[] args)
{
try{
new SampleDOM().CreateXMLFile();
new SampleDOM().GetStringFromXML();
}catch(Exception exception){
exception.printStackTrace();
}
}
//XMLファイルを作成する
public final void CreateXMLFile()
{
try{
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element parent = doc.createElement("伝票");
Element sub = doc.createElement("商品");
Element leaf1 = doc.createElement("商品名");
Element leaf2 = doc.createElement("単価");
Element leaf3 = doc.createElement("数量");
leaf1.appendChild(doc.createTextNode("テレビ"));
sub.appendChild(leaf1);
leaf2.appendChild(doc.createTextNode("15000"));
sub.appendChild(leaf2);
leaf3.appendChild(doc.createTextNode("14"));
sub.appendChild(leaf3);
parent.appendChild(sub);
sub = doc.createElement("商品");
leaf1 = doc.createElement("商品名");
leaf2 = doc.createElement("単価");
leaf3 = doc.createElement("数量");
leaf1.appendChild(doc.createTextNode("ラジオ"));
sub.appendChild(leaf1);
leaf2.appendChild(doc.createTextNode("3500"));
sub.appendChild(leaf2);
leaf3.appendChild(doc.createTextNode("6"));
sub.appendChild(leaf3);
parent.appendChild(sub);
sub = doc.createElement("商品");
leaf1 = doc.createElement("商品名");
leaf2 = doc.createElement("単価");
leaf3 = doc.createElement("数量");
leaf1.appendChild(doc.createTextNode("ビデオ"));
sub.appendChild(leaf1);
leaf2.appendChild(doc.createTextNode("21000"));
sub.appendChild(leaf2);
leaf3.appendChild(doc.createTextNode("4"));
sub.appendChild(leaf3);
parent.appendChild(sub);
doc.appendChild(parent);
OutputStream os =
new BufferedOutputStream(
new FileOutputStream("SampleDOM.xml"));
TransformerFactory tf =
TransformerFactory.newInstance();
Transformer tran = tf.newTransformer();
tran.setOutputProperty("encoding", "Shift_JIS");
tran.setOutputProperty("indent", "yes");
tran.transform(new DOMSource(doc),
new StreamResult(os));
os.flush();
os.close();
}catch(ParserConfigurationException e){
e.printStackTrace();
}catch(TransformerConfigurationException e){
e.printStackTrace();
}catch(TransformerException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
//CreateXMLFileで作成したXMLファイルからデータを取得して計算する
public final void GetStringFromXML()
{
try{
int tvPrice = 0;
int radioPrice = 0;
int videoPrice = 0;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("SampleDOM.xml");
Element element1 = doc.getDocumentElement();
NodeList nodelist1 = element1.getElementsByTagName("商品");
//商品の数だけループをまわす
for(int nShouhin = 0 ;
nShouhin < nodelist1.getLength() ; nShouhin++){
String str = new java.lang.String();
int price = 0;
int number = 0;
Element element2 = (Element)nodelist1.item(nShouhin);
//商品名の取得
NodeList nodelist2 =
element2.getElementsByTagName("商品名");
Node node1 = nodelist2.item(0);
if(node1.getNodeType() == Node.ELEMENT_NODE){
Element element3 = (Element)node1;
NodeList nodelist3 = element3.getChildNodes();
Node node2 = nodelist3.item(0);
str = node2.getNodeValue();
}
//商品名に対する単価の取得
nodelist2 = element2.getElementsByTagName("単価");
node1 = nodelist2.item(0);
if(node1.getNodeType() == Node.ELEMENT_NODE){
Element element3 = (Element)node1;
NodeList nodelist3 = element3.getChildNodes();
Node node2 = nodelist3.item(0);
price = Integer.parseInt(node2.getNodeValue());
}
//商品名に対する数量の取得
nodelist2 = element2.getElementsByTagName("数量");
node1 = nodelist2.item(0);
if(node1.getNodeType() == Node.ELEMENT_NODE){
Element element3 = (Element)node1;
NodeList nodelist3 = element3.getChildNodes();
Node node2 = nodelist3.item(0);
number = Integer.parseInt(node2.getNodeValue());
}
//各商品の小計を計算する
if(str.equals("テレビ")){
tvPrice = price * number;
}else if(str.equals("ラジオ")){
radioPrice = price * number;
}else if(str.equals("ビデオ")){
videoPrice = price * number;
}
}
int sum = tvPrice + radioPrice + videoPrice;
System.out.println("合計は" + sum + "円です。");
}catch(ParserConfigurationException e){
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}