XML Utility Class

Source: Internet
Author: User
Tags format
XML package com.elink.util;

/*
* <p>company: Linco software www.elingke.com </p>
* @author Liubaojun
* @version 1.0
* Created on 2004-11-29
* FROM ELINKBSP Partial source code
*/

Import java.io.*;
Import java.util.*;
Import javax.xml.parsers.*;
Import javax.xml.transform.*;
Import javax.xml.transform.dom.*;
Import javax.xml.transform.stream.*;

Import org.w3c.dom.*;
Import org.xml.sax.*;

public class Xmlutil
{
public static synchronized Document newdocument ()
{
Document doc = null;
Try
{
Documentbuilder db = Documentbuilderfactory.newinstance ().
Newdocumentbuilder ();
doc = Db.newdocument ();
}
catch (Exception e)
{
Logutil.logexception (e);
}
return doc;
}

public static synchronized Element createrootelement ()
{
Element rootelement = null;
Try
{
Documentbuilder db = Documentbuilderfactory.newinstance ().
Newdocumentbuilder ();
Document doc = Db.newdocument ();
RootElement = Doc.getdocumentelement ();
}
catch (Exception e)
{
E.printstacktrace ();
}
return rootelement;
}

public static synchronized Element getrootelement (String fileName)
{
if (FileName = = NULL | | filename.length () = 0)
{
return null;
}
Try
{
Element rootelement = null;
FileInputStream fis = new FileInputStream (fileName);
rootelement = Getrootelement (FIS);
Fis.close ();
return rootelement;
}
catch (Exception e)
{
return null;
}
}

public static synchronized Element getrootelement (InputStream is)
{
if (is = = null)
{
return null;
}
Element rootelement = null;
Try
{
Documentbuilder db = Documentbuilderfactory.newinstance ().
Newdocumentbuilder ();
Document doc = Db.parse (IS);
RootElement = Doc.getdocumentelement ();
}
catch (Exception e)
{
E.printstacktrace ();
}
return rootelement;
}

public static synchronized Element getrootelement (InputSource is)
{
if (is = = null)
{
return null;
}
Element rootelement = null;
Try
{
Documentbuilder db = Documentbuilderfactory.newinstance ().
Newdocumentbuilder ();
Document doc = Db.parse (IS);
RootElement = Doc.getdocumentelement ();
}
catch (Exception e)
{
E.printstacktrace ();
}
return rootelement;
}

public static synchronized element[] Getchildelements (element Element)
{
if (element = = null)
{
return null;
}
Vector childs = new vector ();
for (Node node = element.getfirstchild (); node!= null;
node = node.getnextsibling ())
{
if (node instanceof Element)
{
Childs.add ((Element) node);
}
}
element[] ELMT = new element[childs.size ()];
Childs.toarray (ELMT);
return ELMT;
}

public static synchronized element[] Getchildelements (element element,
String childname)
{
if (element = = NULL | | childname = NULL | | childname.length () = 0)
{
return null;
}
Vector childs = new vector ();
for (Node node = element.getfirstchild (); node!= null;
node = node.getnextsibling ())
{
if (node instanceof Element)
{
if (Node.getnodename (). Equals (ChildName))
{
Childs.add ((Element) node);
}
}
}
element[] ELMT = new element[childs.size ()];
Childs.toarray (ELMT);
return ELMT;
}

public static synchronized node[] Getchildnodes (node node)
{
if (node = null)
{
return null;
}
Vector childs = new vector ();
for (Node n = node.getfirstchild (); n!= null;
n = n.getnextsibling ())
{
Childs.add ((Element) n);
}
node[] ChildNodes = new element[childs.size ()];
Childs.toarray (childnodes);
return childnodes;
}

public static synchronized element GetChildElement (element element,
String childname)
{
if (element = = NULL | | childname = NULL | | childname.length () = 0)
{
return null;
}
Element childs = null;
for (Node node = element.getfirstchild (); node!= null;
node = node.getnextsibling ())
{
if (node instanceof Element)
{
if (Node.getnodename (). Equals (ChildName))
{
Childs = (Element) node;
Break
}
}
}
return childs;
}

public static synchronized element GetChildElement (element Element)
{
if (element = = null)
{
return null;
}
Element childs = null;
for (Node node = element.getfirstchild (); node!= null;
node = node.getnextsibling ())
{
if (node instanceof Element)
{
Childs = (Element) node;
Break
}
}
return childs;
}

public static synchronized string[] Getelenentvalues (element Element)
{
if (element = = null)
{
return null;
}
Vector childs = new vector ();
for (Node node = element.getfirstchild (); node!= null;
node = node.getnextsibling ())
{
if (node instanceof Text)
{
Childs.add (Node.getnodevalue ());
}
}
String[] values = new string[childs.size ()];
Childs.toarray (values);
return values;
}

public static synchronized String Getelenentvalue (element Element)
{
if (element = = null)
{
return null;
}
String retnstr = null;
for (Node node = element.getfirstchild (); node!= null;
node = node.getnextsibling ())
{
if (node instanceof Text)
{
String str = Node.getnodevalue ();
if (str = NULL | | str.length () = 0
|| Str.trim (). Length () = 0)
{
Continue
}
Else
{
Retnstr = str;
Break
}
}
}
return retnstr;
}

public static synchronized element Findelementbyname (element e, String name)
{
if (e = null | | | name = NULL | | name.length () = 0)
{
return null;
}
String nodename = null;
element[] Childs = getchildelements (e);
for (int i = 0; i < childs.length; i++)
{
NodeName = Childs[i].getnodename ();
if (Name.equals (nodename))
{
return childs[i];
}
}
for (int i = 0; i < childs.length; i++)
{
Element retn = Findelementbyname (childs[i], name);
if (RETN!= null)
{
return RETN;
}
}
return null;
}
public static synchronized element findelementbyattr (element E, String attrname,
String attrval)
{
Return Findelementbyattr (E, Attrname, Attrval, true);
}

public static synchronized element findelementbyattr (element E, String attrname,
String Attrval, Boolean dept)
{
if (e = null | | attrname = NULL | | attrname.length () = 0
|| Attrval = = NULL | | Attrval.length () = = 0)
{
return null;
}
String tmpvalue = null;
element[] Childs = getchildelements (e);
for (int i = 0; i < childs.length; i++)
{
Tmpvalue = Childs[i].getattribute (attrname);
if (Attrval.equals (Tmpvalue))
{
return childs[i];
}
}
if (dept)
{
for (int i = 0; i < childs.length; i++)
{
Element retn = findelementbyattr (Childs[i], attrname, attrval);
if (RETN!= null)
{
return RETN;
}
}
}
return null;
}

public static synchronized String Formatxml (Element e)
{
Return Formatxml (e, 0);
}

/**
* Format XML output string.
*/
public static synchronized String Formatxml (Element e, int indent)
{
indent++;
for (Node n = e.getfirstchild (); n!= null; n = n.getnextsibling ())
{
Appendindent (e, N, indent);
if (!n.getnodename (). Equals ("#text")
{
Formatxml ((Element) n, indent);
}
}
indent--;
Appendindent (e, indent);
return e.tostring ();
}

/**
* Inserts a format representation before the specified node.
*/
private static synchronized void Appendindent (Element e, Node pos, int indent)
{
Document doc = E.getownerdocument ();
if (indent = 0)
{
E.insertbefore (Doc.createtextnode ("\ n"), POS);
}
for (int i = 0; i < indent; i++)
{
if (i = = 0)
{
E.insertbefore (Doc.createtextnode ("\n\t"), POS);
}
Else
{
E.insertbefore (Doc.createtextnode ("T"), POS);
}
}
}

/**
* Append format representation.
*/
private static synchronized void Appendindent (Element e, int indent)
{
Document doc = E.getownerdocument ();
if (indent = 0)
{
E.appendchild (Doc.createtextnode ("\ n"));
}
for (int i = 0; i < indent; i++)
{
if (i = = 0)
{
E.appendchild (Doc.createtextnode ("\n\t"));
}
Else
{
E.appendchild (Doc.createtextnode ("T"));
}
}
}

public static synchronized void setattribute (Element e, string name, String value)
{
if (e = null | | | name = NULL | | name.length () = 0 | | | value = NULL
|| Value.length () = = 0)
Return
Else
E.setattribute (name, value);
}

public static synchronized String getattribute (Element E, string name)
{
Return GetAttribute (E, name, NULL);
}
public static synchronized String getattribute (Element E, string name, String defval)
{
if (e = null | | | name = NULL | | name.length () = 0)
return defval;
Else
return E.getattribute (name);
}

public void Transformerwrite (Element doc, String filename) throws Exception
{
Domsource doms = new Domsource (DOC);
File F = new file (filename);
Streamresult sr = new Streamresult (f);
Transformerwrite (Doms, SR);
}

public void Transformerwrite (Element doc, file file) throws Exception
{
Domsource doms = new Domsource (DOC);
Streamresult sr = new Streamresult (file);
Transformerwrite (Doms, SR);
}

public void Transformerwrite (Element doc, OutputStream OutStream) throws Exception
{
Domsource doms = new Domsource (DOC);
Streamresult sr = new Streamresult (OutStream);
Transformerwrite (Doms, SR);
}

public void Transformerwrite (Element doc, Writer outwriter) throws Exception
{
Domsource doms = new Domsource (DOC);
Streamresult sr = new Streamresult (outwriter);
Transformerwrite (Doms, SR);
}

public void Transformerwrite (Domsource doms, Streamresult Sr) throws Exception
{
Transformerfactory tf = Transformerfactory.newinstance ();
Transformer t = Tf.newtransformer ();
T.setoutputproperty (outputkeys.encoding, "GBK");
T.transform (Doms, SR);
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.