java读取xml一些方法

package com.qeesoo.util;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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

import com.qeesoo.exception.NotFindFileException;
import com.qeesoo.exception.NotFindNodeException;

public class XmlUtil {

?? ?private Document doc;

?? ?private Element root;

?? ?private File configFile;
?? ?

??? public static void main(String[] args) throws Exception {
??? ??? // TODO Auto-generated method stub

??? ??? XmlUtil px = new XmlUtil("./testconf.xml");
??? ??? Node node=px.getNode("movetables");
??? ??? Node childNodes=px.getNode(node, new String[]{"source","target"}, new String[]{"qeesoo_en","qeesoo_en2"});
??? ??? NodeList nodelist=childNodes.getChildNodes();
??? ??? for(int i=0;i<nodelist.getlength();i++){<br> ??? ??? ??? Node childNode=nodelist.item(i);
??? ??? ???
??? ??? ??? if(childNode.getNodeType()==Node.ELEMENT_NODE){
??? ??? ??? ??? Node childNodeAttribute=childNode.getAttributes().getNamedItem("sourcetable");
??? ??? ??? ??? if(childNodeAttribute!=null){
??? ??? ??? ??? ??? String className=childNodeAttribute.getNodeValue();
??? ??? ??? ??? ??? System.out.println(className);
??? ??? ??? ??? }
??? ??? ??? ??? childNodeAttribute=childNode.getAttributes().getNamedItem("targettable");
??? ??? ??? ??? if(childNodeAttribute!=null){
??? ??? ??? ??? ??? String className=childNodeAttribute.getNodeValue();
??? ??? ??? ??? ??? System.out.println(className);
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }
??? ???
??? ??? Node datanode=px.getNode("fields");
??? ??? NodeList nodelistd=datanode.getChildNodes();
??? ??? for(int i=0;i<nodelistd.getlength();i++){<br> ??? ??? ??? Node childNode=nodelistd.item(i);
??? ??? ??? System.out.println(px.getAttributeValue(childNode, "name"));
??? ??? }
??? ???
??? ??? px.getAttributeValue(datanode, "id");
??? ??? //px.addNode(childNodes, "xxxxx", new String[]{"param1","param2","param3"}, new String[]{"add","update","delete"});
??? ???
??? }

??? //初始化xml
???
??? public void init(String fileName) throws NotFindFileException, Exception {
??? ???
??? ??? configFile = new File(fileName);
??? ???
??? ??? if (!configFile.exists()) {
??? ??? ??? throw new NotFindFileException("Xml is not Found!");
??? ??? }
??? ???
??? ??? DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
??? ??? DocumentBuilder builder = factory.newDocumentBuilder();
??? ??? doc = builder.parse(configFile);
??? ??? doc.normalize();
??? ??? root = doc.getDocumentElement();
??? }
???
??? public XmlUtil(String fileName)throws NotFindFileException, Exception {
??? ??? this.init(fileName);
??? }
???
???
???
??? public Node getNode(String nodeName) throws NotFindNodeException{
??? ??? return this.getNode(root, nodeName);
??? }
???
???
???
??? public Node getNode(Node node,String nodeName) throws NotFindNodeException{
??? ???
??? ??? if(node==null){
??? ??? ??? throw new NotFindNodeException("Not Find this node!");
??? ??? }
??? ???
??? ??? NodeList subList = node.getChildNodes();
??? ??? String nodeValue;
??? ??? if (subList != null) {
??? ??? ??? for (int i = 0; i < subList.getLength(); i++) {
??? ??? ??? ??? Node pros = subList.item(i);
??? ??? ??? ??? if (pros.getNodeType() == Node.ELEMENT_NODE) {
??? ??? ??? ??? ??? nodeValue=pros.getNodeName();
??? ??? ??? ??? ??? if(nodeValue.equalsIgnoreCase(nodeName)){
??? ??? ??? ??? ??? ??? return pros;
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }
??? ??? return null;
??? }
???
???
???
??? public Node getNode(String[] nodeAttributeNames, String[] nodeAttributeValues) throws NotFindNodeException{
??? ???
??? ??? if(nodeAttributeNames.length!=nodeAttributeValues.length){
??? ??? ??? throw new IllegalArgumentException("nodeAttributeNames's length is not equals nodeAttributeValues's length");
??? ??? }
??? ???
??? ??? return this.getNode(root, nodeAttributeNames, nodeAttributeValues);
??? }
???
???
???
??? public Node getNode(Node node,String[] nodeAttributeNames, String[] nodeAttributeValues) throws NotFindNodeException{
??? ???
??? ??? if(nodeAttributeNames.length!=nodeAttributeValues.length){
??? ??? ??? throw new IllegalArgumentException("nodeAttributeNames's length is not equals nodeAttributeValues's length");
??? ??? }
??? ???
??? ??? if(node==null){
??? ??? ??? throw new NotFindNodeException("Not Find this node!");
??? ??? }
??? ???
??? ??? NodeList subList = node.getChildNodes();
??? ??? Node tmp;
??? ??? int argumentsLength=nodeAttributeNames.length;
??? ??? String attrNodeValue;
??? ??? if (subList != null) {
??? ??? ??? for (int i = 0; i < subList.getLength(); i++) {
??? ??? ??? ??? Node pros = subList.item(i);
??? ??? ??? ??? boolean isCorrentNode=true;//判断结点是否为正确的结点
??? ??? ??? ??? if (pros.getNodeType() == Node.ELEMENT_NODE) {
??? ??? ??? ??? ??? for(int j=0;j<argumentslength;j++){<br> ??? ??? ??? ??? ??? ??? tmp = pros.getAttributes().getNamedItem(nodeAttributeNames[j]);
??? ??? ??? ??? ??? ??? if(tmp!=null){
??? ??? ??? ??? ??? ??? ??? attrNodeValue=tmp.getNodeValue();
??? ??? ??? ??? ??? ??? ??? if (nodeAttributeValues[j].equalsIgnoreCase(attrNodeValue)) {
??? ??? ??? ??? ??? ??? ??? ??? isCorrentNode&=true;
??? ??? ??? ??? ??? ??? ??? }else{
??? ??? ??? ??? ??? ??? ??? ??? isCorrentNode&=false;
??? ??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? }else{
??? ??? ??? ??? ??? ??? ??? isCorrentNode&=false;
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? if(isCorrentNode){
??? ??? ??? ??? ??? ??? return pros;
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }
??? ??? return null;
??? }
???
???
???
??? public void addNode(Node node,String childNodeName,String childNodeValue) throws Exception {
??? ???
??? ??? if(node==null){
??? ??? ??? throw new NotFindNodeException("Not Find this node!");
??? ??? }
??? ???
??? ??? Text textChildNodeValue = doc.createTextNode(childNodeValue);
??? ??? Element elementChildNode =doc.createElement(childNodeName);
??? ??? elementChildNode.appendChild(textChildNodeValue);
??? ??? node.appendChild(elementChildNode);
??? ??? this.writeInputFile();
??? ???
??? }
???
???
???
??? public void addNode(Node node,String childNodeName,String[] childNodeAttributeKeys,String[] childNodeAttributeValues) throws Exception {
??? ???
??? ??? Element elementChildNode =doc.createElement(childNodeName);
??? ???
??? ??? if(childNodeAttributeKeys.length!=childNodeAttributeValues.length){
??? ??? ??? throw new IllegalArgumentException("childNodeAttributeKeys's length is not equals childNodeAttributeValues's length");
??? ??? }
??? ???
??? ??? if(node==null){
??? ??? ??? throw new NotFindNodeException("Not Find this node!");
??? ??? }
??? ???
??? ??? for(int i=0;i<childnodeattributekeys.length;i++){<br> ??? ??? ???
??? ??? ??? Attr attribute=doc.createAttribute(childNodeAttributeKeys[i]);
??? ??? ??? Text attributeValue=doc.createTextNode(childNodeAttributeValues[i]);
??? ??? ??? attribute.appendChild(attributeValue);
??? ??? ??? elementChildNode.setAttributeNode(attribute);
??? ??? ???
??? ??? }
??? ???
??? ??? node.appendChild(elementChildNode);
??? ??? this.writeInputFile();
??? }
???
???

??? private void writeInputFile() throws Exception {
??? ??? TransformerFactory tFactory = TransformerFactory.newInstance();
??? ??? Transformer transformer = tFactory.newTransformer();
??? ??? //transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312");
??? ??? transformer.setOutputProperty(OutputKeys.INDENT,"yes");
??? ??? transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS,"yes");
??? ??? //设置写入文件的格式
??? ??? transformer.setOutputProperty(OutputKeys.METHOD,"xml");
??? ??? DOMSource source = new DOMSource(doc);
??? ??? StreamResult result = new StreamResult(configFile);
??? ??? transformer.transform(source, result);
??? }
???
???
???
??? public String getAttributeValue(Node node,String attributeKey) {???
??? ??? if(node==null){
??? ??? ??? return "";
??? ??? }
??? ???
??? ??? if(node.getNodeType()==Node.ELEMENT_NODE){???
??? ??? ??? Node attributeNode=node.getAttributes().getNamedItem(attributeKey);
??? ??? ??? if(attributeNode!=null){
??? ??? ??? ??? return attributeNode.getNodeValue();
??? ??? ??? }
??? ??? }
??? ???
??? ??? return "";
??? }
???
???
???
??? public String getNodeValue(Node node) {???
??? ??? if(node==null){
??? ??? ??? return "";
??? ??? }
??? ???
??? ??? if(node.getNodeType()==Node.ELEMENT_NODE){???
??? ??? ??? return node.getFirstChild().getNodeValue();
??? ??? }
??? ???
??? ??? return "";
??? }

}
?

你可能感兴趣的:(java,xml,J#)