📄 xmlprocess.java
字号:
package com.rochoc.xml.Impl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.XSLTransformException;
import org.jdom.transform.XSLTransformer;
import org.jdom.Attribute;
import com.rochoc.xml.Interf.IXMLProcess;
import com.rochoc.xml.tools.GetFormat;
import com.rochoc.xml.tools.PropSet;
/**
* @author luoc
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*
* 功能:XML文件处理
*
* @author luoc
* @since 1.0
* @version 2005-02-05
*/
public class XMLProcess implements IXMLProcess
{
/**
* 方法描述:构造函数
*/
public XMLProcess()
{
Path="default.xml";
PropSet.getInstance();
if(PropSet.getValueAt("XMLFile")!=null)
{
Path=PropSet.getValueAt("XMLFile");
}
//System.out.println("Path="+Path);
setDocument(Path);
}
/**
* 方法描述:构造函数
* @param path String
*/
public XMLProcess(String path)
{
Path=path;
setDocument(Path);
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#setDocument(Document)
*/
public void setDocument(Document doc)
{
Doc=doc;
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#setDocument(String)
*/
public void setDocument(String path)
{
try
{
SAXBuilder builder=new SAXBuilder();
Doc=builder.build(path);
}catch(JDOMException xe)
{
System.out.println("进行XML文件"+Path+"分析失败,"+xe);
}catch(IOException ie)
{
System.out.println("读取XML文件"+Path+"失败,"+ie);
}
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getDocument()
*/
public Document getDocument()
{
return Doc;
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getRoot()
*/
public Element getRoot()
{
return Doc.getRootElement();
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getChildren(Element)
*/
public List getChildren(Element node)
{
return node.getChildren();
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getChildren(Element, String)
*/
public List getChildren(Element node, String name)
{
return node.getChildren(name);
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getChildren(Element, String, String)
*/
public List getChildren(Element node, String name, String ns)
{
Namespace Ns=Namespace.getNamespace(ns);
return node.getChildren(name,Ns);
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getChild(Element, String)
*/
public Element getChild(Element node, String name)
{
return node.getChild(name);
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getChild(Element, String, String)
*/
public Element getChild(Element node, String name, String ns)
{
Namespace Ns=Namespace.getNamespace(ns);
return node.getChild(name,Ns);
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getTextVal(Element)
*/
public String getTextVal(Element node)
{
return node.getText();
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getAttrVal(Element, String)
*/
public String getAttrVal(Element node, String attr)
{
Attribute att=node.getAttribute(attr);
return att.getValue();
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getListByAttr(Element, String, String)
*/
public List getListByAttr(Element node, String attr, String attrVal)
{
ArrayList ret=new ArrayList();
List chdren=node.getChildren();
Iterator chdI=chdren.iterator();
while(chdI.hasNext())
{
Element element=(Element)chdI.next();
String val=element.getAttributeValue(attr);
if(val!=null && val.equalsIgnoreCase(attrVal))
{
ret.add(element);
}
}
return ret;
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getListByAttrLike(Element, String, String)
*/
public List getListByAttrLike(Element node, String attr, String attrVal)
{
ArrayList ret=new ArrayList();
List chdren=node.getChildren();
Iterator chdI=chdren.iterator();
while(chdI.hasNext())
{
Element element=(Element)chdI.next();
String val=element.getAttributeValue(attr);
if(val!=null && val.indexOf(attrVal)>=0)
{
ret.add(element);
}
}
return ret;
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getListByText(Element, String)
*/
public List getListByText(Element node, String textVal)
{
ArrayList ret=new ArrayList();
List chdren=node.getChildren();
Iterator chdI=chdren.iterator();
while(chdI.hasNext())
{
Element element=(Element)chdI.next();
String val=element.getTextTrim();
if(val!=null && val.equalsIgnoreCase(textVal))
{
ret.add(element);
}
}
return ret;
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#getListByTextLike(Element, String)
*/
public List getListByTextLike(Element node, String textVal)
{
ArrayList ret=new ArrayList();
List chdren=node.getChildren();
Iterator chdI=chdren.iterator();
while(chdI.hasNext())
{
Element element=(Element)chdI.next();
String val=element.getTextTrim();
if(val!=null && val.indexOf(textVal)>=0)
{
ret.add(element);
}
}
return ret;
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#setAttrVal(Element, String, String)
*/
public void setAttrVal(Element node, String attr, String attrVal)
{
node.setAttribute(attr,attrVal);
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#setElementText(Element, String)
*/
public void setElementText(Element node, String val)
{
node.setText(val);
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#addElement(Element, Element)
*/
public void addElement(Element parent, Element child)
{
parent.addContent(child);
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#addElement(Element, String, String)
*/
public void addElement(Element parent, String nodeName, String nodeVal)
{
Element element = new Element(nodeName);
element.setText(nodeVal);
parent.addContent(element);
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#toXSLTransform(String, String)
*/
public Document toXSLTransform(String docname, String sheetname)
{
try
{
SAXBuilder builder=new SAXBuilder();
Document doc=builder.build(docname);
return toXSLTransform(doc,sheetname);
}catch(JDOMException xe)
{
System.out.println("进行XML文件分析失败,"+xe);
}catch(IOException ie)
{
System.out.println("读取XML文件失败,"+ie);
}
return null;
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#toXSLTransform(Document, String)
*/
public Document toXSLTransform(Document doc, String sheetname)
{
try
{
XSLTransformer transformer=new XSLTransformer(sheetname);
Document docT=transformer.transform(doc);
return docT;
}catch(XSLTransformException xe)
{
System.out.println("进行XSL转换失败,"+xe);
}
return null;
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#saveXMLFile()
*/
public void saveXMLFile()
{
try
{
File f=new File(Path);
FileOutputStream fis=new FileOutputStream(f);
XMLOutputter output=new XMLOutputter();
GetFormat xFmt=new GetFormat();
output.setFormat(xFmt.getFormatter());
output.output(Doc,fis);
}catch(IOException ie)
{
System.out.println("保存XML文件"+Path+"失败,"+ie);
}
}
/**
* @see com.rochoc.xml.Interf.IXMLProcess#saveXMLFile(Document, String)
*/
public void saveXMLFile(Document doc, String file)
{
try
{
File f=new File(file);
FileOutputStream fis=new FileOutputStream(f);
XMLOutputter output=new XMLOutputter();
GetFormat xFmt=new GetFormat();
output.setFormat(xFmt.getFormatter());
output.output(doc,fis);
}catch(IOException ie)
{
System.out.println("保存XML文件"+file+"失败,"+ie);
}
}
/************全局变量*************/
private Document Doc=null;//XML文件对应的Doc
private String Path=null;//XML文件名和路径
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -