to:zcjl():嘿嘿不好意思写的有点直白了,以前写过dom的只不过过来跟各位大虾交流一下:)没别的意思,都是做这个怎么能说大家是廉价劳动力那,我在这里跟你说声"对不起"哦我真不是那个意思不过是方便大家以后的应用这样我贴一个用dom封装的javabean 供大家研究希望有人能在我写出jdom的封装后,也能把自己写的贴出来,方便大家交流;) 楼上的我这么说你该不反对吧
解决方案 »
- webwork
- SSH整合就这样整合而已
- dwr[org.directwebremoting.dwrp.BaseCallMarshaller]Erroring: batchId[3]
- 小女子在这跪求CSDN上的高手, 刚入行,水平很烂,请帮忙.在线等servlet的查询问题
- SpringMVC求指教100分!!sessionFactory.getCurrentSession().delete无法删除
- 心情不好, 送分
- 为什么在struts中得不到JAVASCript的路径呢?
- 建立Linux下的j2ee开发环境需要哪些DD啊??
- 关于mysql的数据库访问
- jmx监控tomcat的获取连接池信息的问题
- 关于datasource!分已经用完,大家帮帮忙啊?
- rs.getString的参数问题
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.lang.System;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.util.Vector;import org.w3c.dom.*;
import org.w3c.dom.Document.*;import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xerces.dom.DocumentImpl;public class XmlApi{ private DocumentBuilderFactory factory ;
private DocumentBuilder bulider;
private Document doc;
/********构造函数********/
public XmlApi(String PATH) throws Exception
{
factory = DocumentBuilderFactory.newInstance();
bulider = factory.newDocumentBuilder();
doc = bulider.parse(PATH);
}
/********取值*******/
public String GetValue(String Node) throws Exception
{
NodeList nl = doc.getElementsByTagName(Node);
String Value = nl.item(0).getFirstChild().getNodeValue().trim();
return Value;
}
/*********根据值查找节点*******/
/***** index >= 0 为找到该节点 ******/
/***** index = -1 找无节点位此值 ******/
/***** index >= 0 为无该节点 ******/
public int Found(String Node , String value) throws Exception
{
int index = -1;
NodeList nl = doc.getElementsByTagName(Node);
int len = nl.getLength();
if(len <= 0)
return -2;
for(int i = 0; i < len; i++)
{
String NodeValue = nl.item(0).getFirstChild().getNodeValue().trim();
if(value.equals(NodeValue))
{
index = i;
break;
}
}
return index;
}
/********修改***********/
public void Mod(String Node, String Value) throws Exception
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element) nl.item(0);
Ele.getFirstChild().setNodeValue(Value);
}
public void Mod(String Node, String Value, int index) throws Exception
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element) nl.item(index);
Ele.getFirstChild().setNodeValue(Value);
}
/*********修改子节点内容**********/
public void ModChild(String Node, String ChildNode, String Value)
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element)nl.item(0);
Ele.getElementsByTagName(ChildNode).item(0).getFirstChild().setNodeValue(Value);
}
public void ModChild(String Node, String ChildNode, String Value, int index)
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element)nl.item(index);
Ele.getElementsByTagName(ChildNode).item(0).getFirstChild().setNodeValue(Value);
}
/*****设置属性****/
public void SetAtt(String Node, String Attribute, String Value)
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element) nl.item(0);
Ele.setAttribute(Attribute, Value);
}
public void SetAtt(String Node, String Attribute, String Value, int index)
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element) nl.item(index);
Ele.setAttribute(Attribute, Value);
}
/********添加文本节点***********/
public void AddTxt(String Node, String Value) throws Exception
{
Text txt = doc.createTextNode(Value);
doc.getElementsByTagName(Node).item(0).appendChild(txt);
} public void AddTxt(String Node, String Value, int index) throws Exception
{
Text txt = doc.createTextNode(Value);
doc.getElementsByTagName(Node).item(index).appendChild(txt);
}
/*********子节点添加文本节点**********/
public void AddChildTxt(String Node, String ChildNode, String Value)
{
Text txt = doc.createTextNode(Value);
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element)nl.item(0);
Ele.getElementsByTagName(ChildNode).item(0).appendChild(txt);
}
public void AddChildTxt(String Node, String ChildNode, String Value, int index)
{
Text txt = doc.createTextNode(Value);
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element)nl.item(index);
Ele.getElementsByTagName(ChildNode).item(0).appendChild(txt);
}
/*********添加节点和值*******************/
public void Add(String ParentNode, String ChildNode, String Value) throws Exception
{
Element Child = doc.createElement(ChildNode);
Text txt = doc.createTextNode(Value);
doc.getElementsByTagName(ParentNode).item(0).appendChild(Child).appendChild(txt);
}
public void Add(String ParentNode, String ChildNode, String Value , int index) throws Exception
{
Element Child = doc.createElement(ChildNode);
Text txt = doc.createTextNode(Value);
doc.getElementsByTagName(ParentNode).item(index).appendChild(Child).appendChild(txt);
}
/************删除节点***********/
public void Del(String DelNode) throws Exception
{
Node ChildNode = doc.getElementsByTagName(DelNode).item(0);
Node ParentNode = ChildNode.getParentNode();
ParentNode.removeChild(ChildNode);
}
public void Del(String DelNode, int index) throws Exception
{
Node ChildNode = doc.getElementsByTagName(DelNode).item(index);
Node ParentNode = ChildNode.getParentNode();
ParentNode.removeChild(ChildNode);
}
/*********把Vector的内容添加导xml中*****************/
public void VectorToXml(Vector Name, Vector Data) throws Exception
{
int k = 0;
int cells = Name.size() - 2;
int rows = Data.size()/cells;
String rootName = (String)Name.elementAt(0);
String nodeName = (String)Name.elementAt(0);
Node root = doc.createElement(rootName);
doc.getDocumentElement().appendChild(root);
for(int i = 0; i < rows; i++)
{
Node node = doc.createElement(nodeName);
root.appendChild(node);
for(int j = 0; j < cells; j++)
{
String memberName = (String)Name.elementAt(j+2);
Node member = doc.createElement(memberName);
String Value = (String)Data.elementAt(k);
Text txt = doc.createTextNode(Value);
node.appendChild(member).appendChild(txt);
k++;
}
}
}
/***********写入文件**************************/ public void Write(String PATH) throws Exception
{
FileWriter writer = new FileWriter(PATH);
OutputFormat outputFormat = new OutputFormat(doc, "GB2312", true);
XMLSerializer serializer = new XMLSerializer(writer, outputFormat);
serializer.serialize(doc);
writer.close();
}
/*************合并XML文件***********/
public void unite(String TPATH) throws Exception
{
try{
Document Tdoc = bulider.parse(TPATH);
Node tmp = doc.importNode(Tdoc.getDocumentElement(),true);
doc.getDocumentElement().appendChild(tmp);
NodeList nl = doc.getElementsByTagName("CName");
String NodeValue = nl.item(0).getFirstChild().getNodeValue().trim();
}
catch(Exception e)
{
}
}
}