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文件的四种方法
java读写xml文件 java读写xml文件 java读写xml文件 java读写xml文件 java读写xml文件 java读写xml文件
利用Java下的IO读取或写入xml及word,txt文件,且能有效去除乱码(很适用,且和POI组建比,少占了很多内存),这是我根据网上很多不同的版本,整合而出的一种最易最好的IO读写文件代码;
文档中有代码案例,详细讲解了使用java读取xml,并附带有关于xml的dtd讲解,里面包含详细的文档和代码案例。和读取xml所需的jar。
介绍了如何通过java来读写xml的方法,其中介绍了几种方法,如:DOM、SAX、JDOM、JAXB、dom4j.
使用Java读取XML配置文件
java读取xml的四种方法,欢迎下载java读取xml的四种方法
简单利用java读取xml中的标签,也是写xml
java 读写xml文件操作, 简单易懂
通过不同的库方法读取修改xml文件。通过java实现。代码易懂
java读写xml的代码
java从xml中读入数据并存入到sqlserver2005数据库中
java读取xml文件的四种方法: 第一种 DOM 实现方法 第二种 DOM4J实现方法 第三种 JDOM实现方法 第四种SAX实现方法
java解析xml字符串,添加属性,更新属性等
java实现读取xml文件内容
java读取xml文件 ,程序自带jar包在mylib下,加入工程即可,工程导入可用,更具自己需要读取xml文件生成所需文件。
java读写xml文件的例子,供参考
Java读取xml文件的四种方法.doc
现在的开发很多很多用到xml文件,java写XML文件和读取XML文件