package com.testfile;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Track {
public static int code;
public static double balance;
public static String pkgCount;
public static double smsPrice;
public static double voicePrice;
public static String signZH;
public static String signEN;
public static String keyspace;
public static void main(String[] X) {
// <?xml version="1.0" encoding="UTF-8"?>
// <resp>
// <code>1000</code>
// <balance>0.855</balance>
// <pkgCount>0</pkgCount>
// <smsPrice>0.045</smsPrice>
// <voicePrice>0.03</voicePrice>
// <signZH>大众</signZH>
// <signEN>dz</signEN>
// </resp>
readPropertyFile("C:\\EclipseSet3.6_V01\\workspace\\NewFile.xml");
System.out.println("code = " + code);
System.out.println("balance = " + balance);
System.out.println("pkgCount = " + pkgCount);
System.out.println("smsPrice = " + smsPrice);
System.out.println("voicePrice = " + voicePrice);
System.out.println("signZH = " + signZH);
System.out.println("signEN = " + signEN);
}
private static void readPropertyFile(String filename) {
File file;
DocumentBuilderFactory factory;
DocumentBuilder builder;
Document doc;
try {
file = new File(filename);
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.parse(file);
} catch(ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException();
} catch (SAXException e) {
throw new RuntimeException();
}
// path開始
NodeList childs = doc.getChildNodes();
for(int i = 0; i < childs.getLength(); i++) {
Node n = childs.item(i);
Element e = (Element)n;
if(e.getNodeType() != Node.ELEMENT_NODE)
continue;
// resp
if(!"resp".equals(e.getTagName()))
continue;
// resp的子要素
NodeList paramNode = n.getChildNodes();
for(int j = 0; j < paramNode.getLength(); j++) {
Node n2 = (Node)paramNode.item(j);
if(n2.getNodeType() != Node.ELEMENT_NODE) continue;
// code
if("code".equals(n2.getNodeName())) {
code = Integer.parseInt(n2.getFirstChild().getNodeValue());
// balance
} else if("balance".equals(n2.getNodeName())) {
balance = Double.parseDouble(n2.getFirstChild().getNodeValue());
// pkg
} else if("pkgCount".equals(n2.getNodeName())) {
pkgCount = n2.getFirstChild().getNodeValue();
} else if ("smsPrice".equals(n2.getNodeName())) {
smsPrice = Double.parseDouble(n2.getFirstChild().getNodeValue());
} else if ("voicePrice".equals(n2.getNodeName())) {
voicePrice = Double.parseDouble(n2.getFirstChild().getNodeValue());
} else if ("signZH".equals(n2.getNodeName())) {
signZH = n2.getFirstChild().getNodeValue();
} else if ("signEN".equals(n2.getNodeName())) {
signEN = n2.getFirstChild().getNodeValue();
}
}
}
}
}
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Track {
public static int code;
public static double balance;
public static String pkgCount;
public static double smsPrice;
public static double voicePrice;
public static String signZH;
public static String signEN;
public static String keyspace;
public static void main(String[] X) {
// <?xml version="1.0" encoding="UTF-8"?>
// <resp>
// <code>1000</code>
// <balance>0.855</balance>
// <pkgCount>0</pkgCount>
// <smsPrice>0.045</smsPrice>
// <voicePrice>0.03</voicePrice>
// <signZH>大众</signZH>
// <signEN>dz</signEN>
// </resp>
readPropertyFile("C:\\EclipseSet3.6_V01\\workspace\\NewFile.xml");
System.out.println("code = " + code);
System.out.println("balance = " + balance);
System.out.println("pkgCount = " + pkgCount);
System.out.println("smsPrice = " + smsPrice);
System.out.println("voicePrice = " + voicePrice);
System.out.println("signZH = " + signZH);
System.out.println("signEN = " + signEN);
}
private static void readPropertyFile(String filename) {
File file;
DocumentBuilderFactory factory;
DocumentBuilder builder;
Document doc;
try {
file = new File(filename);
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.parse(file);
} catch(ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException();
} catch (SAXException e) {
throw new RuntimeException();
}
// path開始
NodeList childs = doc.getChildNodes();
for(int i = 0; i < childs.getLength(); i++) {
Node n = childs.item(i);
Element e = (Element)n;
if(e.getNodeType() != Node.ELEMENT_NODE)
continue;
// resp
if(!"resp".equals(e.getTagName()))
continue;
// resp的子要素
NodeList paramNode = n.getChildNodes();
for(int j = 0; j < paramNode.getLength(); j++) {
Node n2 = (Node)paramNode.item(j);
if(n2.getNodeType() != Node.ELEMENT_NODE) continue;
// code
if("code".equals(n2.getNodeName())) {
code = Integer.parseInt(n2.getFirstChild().getNodeValue());
// balance
} else if("balance".equals(n2.getNodeName())) {
balance = Double.parseDouble(n2.getFirstChild().getNodeValue());
// pkg
} else if("pkgCount".equals(n2.getNodeName())) {
pkgCount = n2.getFirstChild().getNodeValue();
} else if ("smsPrice".equals(n2.getNodeName())) {
smsPrice = Double.parseDouble(n2.getFirstChild().getNodeValue());
} else if ("voicePrice".equals(n2.getNodeName())) {
voicePrice = Double.parseDouble(n2.getFirstChild().getNodeValue());
} else if ("signZH".equals(n2.getNodeName())) {
signZH = n2.getFirstChild().getNodeValue();
} else if ("signEN".equals(n2.getNodeName())) {
signEN = n2.getFirstChild().getNodeValue();
}
}
}
}
}