package com.thtf.ezone.ezoa.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Comparator;
import org.w3c.dom.*;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
public class OrderXML {
/**
* @param args
*/
private Document xmldoc = null;
private Element root = null;
private String filename = null;
private String base = null;
public void setBase(String base) {
this.base = new File(base).getAbsolutePath();
}
public OrderXML(){}
public void generXML(HttpServletRequest request ) {
String path=request.getContextPath();
System.out.println(path);
OrderXML order=new OrderXML();
order.CreateXML("D:/easyway/apache-tomcat-6.0.14/webapps/ezone/documents/share.xml", "D:/easyway/apache-tomcat-6.0.14/webapps/ezone/documents/share/文件夹");
}
public OrderXML(String filename) {
this.filename = filename;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
xmldoc = db.parse(new File(filename));
root = xmldoc.getDocumentElement();
} catch (Exception e) {
e.printStackTrace();
}
}
public void CreateXML(String filename,String foldername) {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db=factory.newDocumentBuilder();
Document xmldoc= db.newDocument();
Element root=xmldoc.createElement("rootdir");
root.setAttribute("version", "1");
// String[] name = foldername.split("\\\\");
// root.setAttribute("name", name[name.length - 1]);
xmldoc.appendChild(root);
File folder=new File(foldername);
addNode(root,folder,xmldoc);
TransformerFactory transFactory=TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("indent", "yes");
DOMSource source=new DOMSource();
source.setNode(xmldoc);
StreamResult result=new StreamResult();
try {
result.setOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}catch(Exception e){e.printStackTrace();}
}
public void addNode(Element node,File folder,Document xmldoc) {
if(folder.isDirectory()){
//目录节点
Element child=xmldoc.createElement("dir");
child.setAttribute("name", folder.getName());
child.setAttribute("id", "1");
child.setAttribute("sortid", "0");
node.appendChild(child);
File[] folders=folder.listFiles();
for(File file:folders){
addNode(child,file,xmldoc);
}
}else{
//文件节点
Element child=xmldoc.createElement("file");
child.setAttribute("name", folder.getName());
child.setAttribute("id", "1");
node.appendChild(child);
}
}
//目录
public void adddir(String filename, String foldername) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document xmldoc = db.parse(new File(filename));
Element root = xmldoc.getDocumentElement();
String[] name = foldername.split("\\\\");
String dir = getDeep2(foldername);
String []dirs= dir.split("/");
String path = "/rootdir";
for(int i=0;i<dirs.length-1;i++){
path+="/" +dirs[i];
}
Element node = (Element) selectSingleNode(path , root);
if(node!=null){
Element child = xmldoc.createElement("dir");
child.setAttribute("name", name[name.length - 1]);
child.setAttribute("id", "1");
child.setAttribute("sortid", "0");
node.appendChild(child);
int id= Integer.parseInt(root.getAttribute("version"));
root.setAttribute("version", String.valueOf(id+1));
}
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("indent", "yes");
DOMSource source = new DOMSource();
source.setNode(xmldoc);
StreamResult result = new StreamResult();
try {
result.setOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void deldir(String filename, String foldername) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document xmldoc = db.parse(new File(filename));
Element root = xmldoc.getDocumentElement();
String[] name = foldername.split("\\\\");
String dir = getDeep2(foldername);
String []dirs= dir.split("/");
String path = "/rootdir";
for(int i=0;i<dirs.length;i++){
path+="/" +dirs[i];
}
Element node = (Element) selectSingleNode(path , root);
//System.out.println(" ************" +name[name.length - 1] +"*********");
if (node != null) {
node.getParentNode().removeChild(node);
int id= Integer.parseInt(root.getAttribute("version"));
root.setAttribute("version", String.valueOf(id+1));
}
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("indent", "yes");
DOMSource source = new DOMSource();
source.setNode(xmldoc);
StreamResult result = new StreamResult();
try {
result.setOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setdir(String filename, String foldername) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document xmldoc = db.parse(new File(filename));
Element root = xmldoc.getDocumentElement();
String[] name = foldername.split("\\\\");
String dir = getDeep2(foldername);
String []dirs= dir.split("/");
String path = "/rootdir";
for(int i=0;i<dirs.length-1;i++){
path+="/" +dirs[i];
}
Element node = (Element) selectSingleNode(path , root);
if (node != null) {
int temp = Integer.parseInt(node.getAttribute("id")) + 1;
node.setAttribute("id", String.valueOf(temp));
node.setAttribute("name", name[name.length - 1]);
int id= Integer.parseInt(root.getAttribute("version"));
root.setAttribute("version", String.valueOf(id+1));
}
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("indent", "yes");
DOMSource source = new DOMSource();
source.setNode(xmldoc);
StreamResult result = new StreamResult();
try {
result.setOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//文件
public void updFile(String filename, String foldername, String file) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document xmldoc = db.parse(new File(filename));
Element root = xmldoc.getDocumentElement();
String[] name = foldername.split("\\\\");
String dir = getDeep2(foldername);
String path = "/rootdir"+dir;
Element node = (Element) selectSingleNode(path, root);
String dirname= ((Element)node.getParentNode()).getAttribute("name");
if (node != null) {
if(dirname.equals(name[name.length - 2])){
Element child = xmldoc.createElement("file");
child.setAttribute("name", file);
child.setAttribute("id", "1");
child.setAttribute("sortid", "0");
node.appendChild(child);
int id= Integer.parseInt(root.getAttribute("version"));
root.setAttribute("version", String.valueOf(id+1));}
}
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("indent", "yes");
DOMSource source = new DOMSource();
source.setNode(xmldoc);
StreamResult result = new StreamResult();
try {
result.setOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void delFile(String filename, String foldername, String file) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document xmldoc = db.parse(new File(filename));
Element root = xmldoc.getDocumentElement();
// String[] name=foldername.split("\\\\");
String[] myfile = file.split("\\\\");
String dir = getDeep2(foldername);
String path = "/rootdir"+dir;
//String path = "/rootdir";
// for (int i = 0; i < deep; i++) {
// path += "/dir";
// }
path += "/file";
Element node = (Element) selectSingleNode(path + "[@name='"
+ myfile[myfile.length - 1] + "']", root);
if (node != null) {
node.getParentNode().removeChild(node);
int id= Integer.parseInt(root.getAttribute("version"));
root.setAttribute("version", String.valueOf(id+1));
}
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("indent", "yes");
DOMSource source = new DOMSource();
source.setNode(xmldoc);
StreamResult result = new StreamResult();
try {
result.setOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void renFile(String filename, String foldername, String file,
String newFile) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document xmldoc = db.parse(new File(filename));
Element root = xmldoc.getDocumentElement();
// String[] myfile=file.split("\\\\");
// int deep = Integer.parseInt(getDeep(foldername));
// String path = "/rootdir";
// for (int i = 0; i < deep; i++) {
// path += "/dir";
// }
String dir = getDeep2(foldername);
String path = "/rootdir"+dir;
path += "/file";
Element node = (Element) selectSingleNode(path + "[@name='" + file
+ "']", root);
if (node != null) {
int temp = Integer.parseInt(node.getAttribute("id")) + 1;
node.setAttribute("id", String.valueOf(temp));
node.setAttribute("name", newFile);
int id= Integer.parseInt(root.getAttribute("version"));
root.setAttribute("version", String.valueOf(id+1));
}
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("indent", "yes");
DOMSource source = new DOMSource();
source.setNode(xmldoc);
StreamResult result = new StreamResult();
try {
result.setOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//设置序号
public void setNode(String filename ,String nodetext, String order) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document xmldoc = db.parse(new File(filename));
Element root = xmldoc.getDocumentElement();
// String[] name=foldername.split("\\\\");
String[] myfile = nodetext.split("\\\\");
String dir = getDeep2(nodetext);
String path = "/rootdir"+dir;
File file= new File(nodetext);
if(!file.isDirectory()){path= path.substring(0, path.lastIndexOf("/"));
path += "/file[@name='"
+ myfile[myfile.length - 1] + "']"; }
// if(myfile[myfile.length - 1].indexOf(".")!=-1){
// path= path.substring(0, path.length()-4);
// path += "/file";}
Element node = (Element) selectSingleNode(path , root);
if (node != null) {
node.setAttribute("sortid", order);
int id= Integer.parseInt(root.getAttribute("version"));
root.setAttribute("version", String.valueOf(id+1));
}
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("indent", "yes");
DOMSource source = new DOMSource();
source.setNode(xmldoc);
StreamResult result = new StreamResult();
try {
result.setOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public int getOrder(String nodetext) {
String[] myfile = nodetext.split("\\\\");
int deep = Integer.parseInt(getDeep(nodetext));
String path = "/rootdir";
for (int i = 0; i < deep; i++) {
path += "/dir";
}
if(myfile[myfile.length - 1].indexOf(".")!=-1){
path += "/file";}
Element node = (Element) selectSingleNode(path + "[@name='"
+ myfile[myfile.length - 1] + "']", root);
if (node != null) {
return Integer.parseInt(node.getAttribute("sortid"));
} else {
return 9999999;
}
}
public String getDeep2(String path) {
String[] deep = null;
if (path != null && path.length() != 0) {
path = path.replace(base + "\\", "");
}
deep = path.split("\\\\");
String dir="";
if (deep != null){
for (int i = 0; i < deep.length; i++) {
dir += "/dir"
+"[@name='"
+ deep[i] + "']";
}
}else{
dir="/dir";
}
return dir;
}
public String getDeep(String path) {
String deep[] = (String[])null;
if (path != null && path.length() != 0) {
path = path.replace((new StringBuilder(String.valueOf(base))).append("\\").toString(), "");
}
deep = path.split("\\\\");
if (deep != null) {
return (new StringBuilder()).append(deep.length).toString();
} else {
return "1";
}
}
public String getFileName(String path) {
String fileName = " ";
if (path != null && path.length() != 0) {
fileName = path.substring(path.lastIndexOf("\\") + 1);
}
return fileName;
}
public static Node selectSingleNode(String express, Object source) {// 查找节点,并返回第一个符合条件节点
Node result = null;
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
try {
result = (Node) xpath
.evaluate(express, source, XPathConstants.NODE);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return result;
}
public void output() {// 将node的XML字符串输出到控制台
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("indent", "yes");
DOMSource source = new DOMSource();
source.setNode(xmldoc);
StreamResult result = new StreamResult();
try {
result.setOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
3. **XML文件构造**:使用编程语言(如Java、Python、C#等)的XML库(如DOM、SAX、XMLStreamWriter等)创建XML文件。这些库提供了API,用于添加、修改和删除XML元素及属性,从而根据模板填充数据。 - DOM...
3. **写入文件**:一旦XML结构构建完成,我们可以将其序列化为字符串,然后写入到本地文件系统。例如,在Python中: ```python import xml.etree.ElementTree as ET # 创建XML结构 root = ET.Element("root") ...
JAVA中生成xml文件到指定路径和上传到ftp服务器到指定路径的方法。
5. **导出与保存**:生成的XML文件可以保存到本地,方便后续的处理或集成到其他系统中。 在实际应用中,这样的工具对于需要处理大量文件信息并以XML格式存储的情况非常有用,例如在数据迁移、文件元数据管理或自动...
`DocumentBuilderFactory`用于创建`DocumentBuilder`对象,这个对象可以解析XML文件并生成`Document`对象。 2. 配置和获取`DocumentBuilder`: ```java DocumentBuilder builder = factory.newDocumentBuilder()...
6. **保存与查看**:生成的XML文件可以保存到本地,用任何支持XML的编辑器或程序进行查看和进一步处理。 此外,对于开发者而言,了解XML的规范和语法是非常重要的。XML文件必须遵循以下原则: - 文件必须以`<?xml ...
标题提到的"本地自动生成映射类和XML文件"就是这样的一个功能,它能够根据数据库表结构自动创建相应的Mapper接口和XML配置文件,使得开发者可以更专注于业务逻辑的实现。 以下是这个过程中的关键知识点: 1. **...
本篇文章将详细讲解如何将XML文件导出为本地的Excel文件,这一过程涉及到数据解析、格式转换以及文件操作等多个关键知识点。 首先,我们需要了解XML的基本结构。XML文件由元素(Elements)、属性(Attributes)、...
在压缩包文件"复件 dom"中,可能包含了示例代码或教程,用于演示如何使用DOM4J和本地DOM解析XML文件。通过查看这些文件,你可以更深入地了解两种方法的具体实现,并在实际项目中选择合适的方式处理XML数据。 总结来...
XML(Extensible Markup Language)是一种...通过以上步骤,开发者可以在本地创建XML文件,自定义保存路径,并对其进行解析,获取所需的数据。理解并熟练运用这些技术,对于IT专业人士在处理结构化数据时是非常重要的。
确保服务器配置允许加载本地文件。 #### 总结 通过以上步骤,可以在`web.xml`中有效地引入并使用外部的XML配置文件。这种方式不仅可以提高代码的可读性和可维护性,还能简化主配置文件的复杂度,便于管理和更新。...
我们将讲解如何使用Pull解析器读取本地XML文件,并生成一个新的、格式不同的XML文件。 Pull解析是一种事件驱动的解析方法,它不像DOM解析那样将整个XML文档加载到内存中,而是按需逐个处理XML元素。这种方法节省了...
另一方面,`objectXmlDecoder`方法打开XML文件,创建一个`XMLDecoder`实例,读取XML数据并将其转换回Java对象。这些对象被添加到一个`ArrayList`中并返回。注意,如果XML文件中包含多个序列化的对象,`XMLDecoder`将...
标题“sax解析xml本地读取xml”指的是使用SAX解析器来读取和解析存储在本地文件系统中的XML文件。这种方式适用于处理那些无法一次性加载到内存中的大型XML文档,或者对于内存有限的环境。 SAX解析的基本工作原理...
5. **导入导出功能**:用户可以导入现有的XML文件,或导出经过编辑的XML到本地。系统支持XML和XSD文件的导入导出,保证了与其他系统的兼容性。 6. **用户界面**:设计了直观的用户界面,采用Windows Forms或WPF技术...
首先,创建一个`File`对象指向XML文件,然后使用`DocumentBuilderFactory`和`DocumentBuilder`来解析文件并构建`Document`对象。 ```java File inputFile = new File("path_to_xml_file"); ...
在客户端,首先创建一个XML文档,然后创建一个用于存储二进制内容的XML节点,接着利用ADO Stream对象读取本地文件并将其内容填充到该节点中。最后,使用XMLHTTP对象通过POST方法将XML文档发送至Web服务器。XMLHTTP...
在iOS开发中,解析XML文件是...这个项目可能涉及到XML数据的展示,例如通过UIWebView加载XML内容并进行解析,或者利用XML数据动态生成用户界面。你可以通过打开并研究这个项目来进一步了解XML解析在实际应用中的实现。
webservice远程调用,返回String数据并生成xml文件到本地工程,在通过SAX解析器把数据解析出来。这是webservice应用的一个简单的例子。根据该例子的思想,可以实现很多功能了。例如把client工程的sayHello方法改为...
为了在ListView控件中显示XML文件内容,我们需要创建ListView项并设置其文本。以下是一个示例: ```csharp ListView listView = new ListView(); listView.View = View.Details; // 遍历XML文件的节点 foreach ...