Xml

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() == 8)
{
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);


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值