package com.huawei.bss.xml;
import java.io.File;
import java.io.StringReader;
import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
* The Class Xml.
*/
public class Xml
{
/** The xml builder. */
private static DocumentBuilder xmlBuilder = null;
static
{
try
{
xmlBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
}
}
/**
* 将XMl文件转换成XMLTree.
*
* @param file the file
* @return the xml tree
* @throws Exception the exception
*/
public static XmlTree parseXml(File file) throws Exception
{
try
{
Element ele = xmlBuilder.parse(file).getDocumentElement();
XmlNode root = makeTreeNode(ele);
return new XmlTree(root);
}
catch (Exception e)
{
throw new Exception(e.getMessage());
}
}
/**
* 将XML字符串转换成XmlTree.
*
* @param xml the xml
* @return the xml tree
* @throws Exception the exception
*/
public static XmlTree parseXml(String xml) throws Exception
{
try
{
InputSource in = new InputSource(new StringReader(xml));
Element ele = xmlBuilder.parse(in).getDocumentElement();
XmlNode root = makeTreeNode(ele);
return new XmlTree(root);
}
catch (Exception e)
{
throw new Exception(e.getMessage());
}
}
/**
* 创建node.
*
* @param element the element
* @return the xml node
*/
private static XmlNode makeTreeNode(Element element)
{
XmlNode node = new XmlNode();
node.setTagName(element.getTagName());
int i;
//设置属性
if (element.hasAttributes())
{
NamedNodeMap attr = element.getAttributes();
for (i = 0; i < attr.getLength(); ++i)
{
node.setAttribute(attr.item(i).getNodeName(), attr.item(i)
.getNodeValue());
}
}
//设置子类Node
if (element.hasChildNodes())
{
NodeList nodes = element.getChildNodes();
for (i = 0; i < nodes.getLength(); ++i)
{
if (nodes.item(i) instanceof Element)
{
XmlNode child = makeTreeNode((Element) nodes.item(i));
child.setParent(node);
node.addNode(child);
}
else
{
if (nodes.item(i).getNodeType() ==
{
continue;
}
String string = nodes.item(i).getTextContent();
if (!(string.trim().equals("")))
{
node.addNode(string.trim());
}
}
}
}
return node;
}
/**
* 创建XML文本.
*
* @param node the node
* @return the string
*/
public static String makeXml(XmlNode node)
{
StringBuilder bf = new StringBuilder();
// bf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
if (node.isRoot())
{
bf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
}
bf.append("<").append(node.getTagName());
if (node.getAttributes() != null)
{
for (Map.Entry set : node.getAttributes().entrySet())
{
String tem = " " + ((String) set.getKey()) + "=" + "\""
+ ((String) set.getValue()) + "\" ";
bf.append(tem);
}
}
bf.append(">\r\n");
if ((node.getChildren() != null) && (node.getChildren().size() > 0))
{
for (Iterator it = node.getChildren().iterator(); it.hasNext();)
{
Object child = it.next();
if (child instanceof XmlNode)
{
bf.append(makeXml((XmlNode) child));
}
else
{
bf.append(child.toString());
}
}
}
bf.append("</").append(node.getTagName()).append(">\r\n");
return bf.toString();
}
public static void main(String[] args)
{
XmlNode rootNode = new XmlNode();
rootNode.setTagName("root");
XmlNode parentNode = new XmlNode();
parentNode.setParent(rootNode);
parentNode.setTagName("students");
XmlNode childnode = new XmlNode();
childnode.setAttribute("name", "小明");
childnode.setAttribute("age", "28");
childnode.setTagName("student");
childnode.setParent(parentNode);
rootNode.addNode(parentNode);
parentNode.addNode(childnode);
XmlTree xmltree=new XmlTree();
xmltree.setRoot(rootNode);
xmltree.showAll();
//
// String xml= Xml.makeXml(rootNode);
//
// System.out.println(xml);
}
}