|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
通过视频学习比传统的大课堂学习更适合成人化的学习规律。有人说大课堂气氛好,学习氛围浓,热闹,可以认识很多人。xmlpackagecom.elink.util;
/*
*<p>Company:凌科软件www.elingke.com</p>
*@authorliubaojun
*@version1.0
*Createdon2004-11-29
*来历于elinkBSP部分源代码
*/
importjava.io.*;
importjava.util.*;
importjavax.xml.parsers.*;
importjavax.xml.transform.*;
importjavax.xml.transform.dom.*;
importjavax.xml.transform.stream.*;
importorg.w3c.dom.*;
importorg.xml.sax.*;
publicclassXmlUtil
{
publicstaticsynchronizedDocumentnewDocument()
{
Documentdoc=null;
try
{
DocumentBuilderdb=DocumentBuilderFactory.newInstance().
newDocumentBuilder();
doc=db.newDocument();
}
catch(Exceptione)
{
LogUtil.logException(e);
}
returndoc;
}
publicstaticsynchronizedElementcreateRootElement()
{
ElementrootElement=null;
try
{
DocumentBuilderdb=DocumentBuilderFactory.newInstance().
newDocumentBuilder();
Documentdoc=db.newDocument();
rootElement=doc.getDocumentElement();
}
catch(Exceptione)
{
e.printStackTrace();
}
returnrootElement;
}
publicstaticsynchronizedElementgetRootElement(StringfileName)
{
if(fileName==null||fileName.length()==0)
{
returnnull;
}
try
{
ElementrootElement=null;
FileInputStreamfis=newFileInputStream(fileName);
rootElement=getRootElement(fis);
fis.close();
returnrootElement;
}
catch(Exceptione)
{
returnnull;
}
}
publicstaticsynchronizedElementgetRootElement(InputStreamis)
{
if(is==null)
{
returnnull;
}
ElementrootElement=null;
try
{
DocumentBuilderdb=DocumentBuilderFactory.newInstance().
newDocumentBuilder();
Documentdoc=db.parse(is);
rootElement=doc.getDocumentElement();
}
catch(Exceptione)
{
e.printStackTrace();
}
returnrootElement;
}
publicstaticsynchronizedElementgetRootElement(InputSourceis)
{
if(is==null)
{
returnnull;
}
ElementrootElement=null;
try
{
DocumentBuilderdb=DocumentBuilderFactory.newInstance().
newDocumentBuilder();
Documentdoc=db.parse(is);
rootElement=doc.getDocumentElement();
}
catch(Exceptione)
{
e.printStackTrace();
}
returnrootElement;
}
publicstaticsynchronizedElement[]getChildElements(Elementelement)
{
if(element==null)
{
returnnull;
}
Vectorchilds=newVector();
for(Nodenode=element.getFirstChild();node!=null;
node=node.getNextSibling())
{
if(nodeinstanceofElement)
{
childs.add((Element)node);
}
}
Element[]elmt=newElement[childs.size()];
childs.toArray(elmt);
returnelmt;
}
publicstaticsynchronizedElement[]getChildElements(Elementelement,
StringchildName)
{
if(element==null||childName==null||childName.length()==0)
{
returnnull;
}
Vectorchilds=newVector();
for(Nodenode=element.getFirstChild();node!=null;
node=node.getNextSibling())
{
if(nodeinstanceofElement)
{
if(node.getNodeName().equals(childName))
{
childs.add((Element)node);
}
}
}
Element[]elmt=newElement[childs.size()];
childs.toArray(elmt);
returnelmt;
}
publicstaticsynchronizedNode[]getChildNodes(Nodenode)
{
if(node==null)
{
returnnull;
}
Vectorchilds=newVector();
for(Noden=node.getFirstChild();n!=null;
n=n.getNextSibling())
{
childs.add((Element)n);
}
Node[]childNodes=newElement[childs.size()];
childs.toArray(childNodes);
returnchildNodes;
}
publicstaticsynchronizedElementgetChildElement(Elementelement,
StringchildName)
{
if(element==null||childName==null||childName.length()==0)
{
returnnull;
}
Elementchilds=null;
for(Nodenode=element.getFirstChild();node!=null;
node=node.getNextSibling())
{
if(nodeinstanceofElement)
{
if(node.getNodeName().equals(childName))
{
childs=(Element)node;
break;
}
}
}
returnchilds;
}
publicstaticsynchronizedElementgetChildElement(Elementelement)
{
if(element==null)
{
returnnull;
}
Elementchilds=null;
for(Nodenode=element.getFirstChild();node!=null;
node=node.getNextSibling())
{
if(nodeinstanceofElement)
{
childs=(Element)node;
break;
}
}
returnchilds;
}
publicstaticsynchronizedString[]getElenentValues(Elementelement)
{
if(element==null)
{
returnnull;
}
Vectorchilds=newVector();
for(Nodenode=element.getFirstChild();node!=null;
node=node.getNextSibling())
{
if(nodeinstanceofText)
{
childs.add(node.getNodeValue());
}
}
String[]values=newString[childs.size()];
childs.toArray(values);
returnvalues;
}
publicstaticsynchronizedStringgetElenentValue(Elementelement)
{
if(element==null)
{
returnnull;
}
StringretnStr=null;
for(Nodenode=element.getFirstChild();node!=null;
node=node.getNextSibling())
{
if(nodeinstanceofText)
{
Stringstr=node.getNodeValue();
if(str==null||str.length()==0
||str.trim().length()==0)
{
continue;
}
else
{
retnStr=str;
break;
}
}
}
returnretnStr;
}
publicstaticsynchronizedElementfindElementByName(Elemente,Stringname)
{
if(e==null||name==null||name.length()==0)
{
returnnull;
}
Stringnodename=null;
Element[]childs=getChildElements(e);
for(inti=0;i<childs.length;i++)
{
nodename=childs[i].getNodeName();
if(name.equals(nodename))
{
returnchilds[i];
}
}
for(inti=0;i<childs.length;i++)
{
Elementretn=findElementByName(childs[i],name);
if(retn!=null)
{
returnretn;
}
}
returnnull;
}
publicstaticsynchronizedElementfindElementByAttr(Elemente,StringattrName,
StringattrVal)
{
returnfindElementByAttr(e,attrName,attrVal,true);
}
publicstaticsynchronizedElementfindElementByAttr(Elemente,StringattrName,
StringattrVal,booleandept)
{
if(e==null||attrName==null||attrName.length()==0
||attrVal==null||attrVal.length()==0)
{
returnnull;
}
StringtmpValue=null;
Element[]childs=getChildElements(e);
for(inti=0;i<childs.length;i++)
{
tmpValue=childs[i].getAttribute(attrName);
if(attrVal.equals(tmpValue))
{
returnchilds[i];
}
}
if(dept)
{
for(inti=0;i<childs.length;i++)
{
Elementretn=findElementByAttr(childs[i],attrName,attrVal);
if(retn!=null)
{
returnretn;
}
}
}
returnnull;
}
publicstaticsynchronizedStringformatXml(Elemente)
{
returnformatXml(e,0);
}
/**
*格局化XML输入串.
*/
publicstaticsynchronizedStringformatXml(Elemente,intindent)
{
indent++;
for(Noden=e.getFirstChild();n!=null;n=n.getNextSibling())
{
appendIndent(e,n,indent);
if(!n.getNodeName().equals("#text"))
{
formatXml((Element)n,indent);
}
}
indent--;
appendIndent(e,indent);
returne.toString();
}
/**
*在指定的节点前拔出格局暗示.
*/
privatestaticsynchronizedvoidappendIndent(Elemente,Nodepos,intindent)
{
Documentdoc=e.getOwnerDocument();
if(indent==0)
{
e.insertBefore(doc.createTextNode("
"),pos);
}
for(inti=0;i<indent;i++)
{
if(i==0)
{
e.insertBefore(doc.createTextNode("
"),pos);
}
else
{
e.insertBefore(doc.createTextNode(" "),pos);
}
}
}
/**
*追加格局暗示.
*/
privatestaticsynchronizedvoidappendIndent(Elemente,intindent)
{
Documentdoc=e.getOwnerDocument();
if(indent==0)
{
e.appendChild(doc.createTextNode("
"));
}
for(inti=0;i<indent;i++)
{
if(i==0)
{
e.appendChild(doc.createTextNode("
"));
}
else
{
e.appendChild(doc.createTextNode(" "));
}
}
}
publicstaticsynchronizedvoidsetAttribute(Elemente,Stringname,Stringvalue)
{
if(e==null||name==null||name.length()==0||value==null
||value.length()==0)
return;
else
e.setAttribute(name,value);
}
publicstaticsynchronizedStringgetAttribute(Elemente,Stringname)
{
returngetAttribute(e,name,null);
}
publicstaticsynchronizedStringgetAttribute(Elemente,Stringname,Stringdefval)
{
if(e==null||name==null||name.length()==0)
returndefval;
else
returne.getAttribute(name);
}
publicvoidtransformerWrite(Elementdoc,Stringfilename)throwsException
{
DOMSourcedoms=newDOMSource(doc);
Filef=newFile(filename);
StreamResultsr=newStreamResult(f);
transformerWrite(doms,sr);
}
publicvoidtransformerWrite(Elementdoc,Filefile)throwsException
{
DOMSourcedoms=newDOMSource(doc);
StreamResultsr=newStreamResult(file);
transformerWrite(doms,sr);
}
publicvoidtransformerWrite(Elementdoc,OutputStreamoutstream)throwsException
{
DOMSourcedoms=newDOMSource(doc);
StreamResultsr=newStreamResult(outstream);
transformerWrite(doms,sr);
}
publicvoidtransformerWrite(Elementdoc,Writeroutwriter)throwsException
{
DOMSourcedoms=newDOMSource(doc);
StreamResultsr=newStreamResult(outwriter);
transformerWrite(doms,sr);
}
publicvoidtransformerWrite(DOMSourcedoms,StreamResultsr)throwsException
{
TransformerFactorytf=TransformerFactory.newInstance();
Transformert=tf.newTransformer();
t.setOutputProperty(OutputKeys.ENCODING,"GBK");
t.transform(doms,sr);
}
}
最初被命名为Oak,目标设定在家用电器等小型系统的编程语言,来解决诸如电视机、电话、闹钟、烤面包机等家用电器的控制和通讯问题。 |
|