`
xinyoulinglei
  • 浏览: 124153 次
社区版块
存档分类
最新评论

xml文件解析(XMLNode)

    博客分类:
  • java
xml 
阅读更多
package com.huawei.bss.xml;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* The Class XmlNode.
*/
public class XmlNode
{
   
    /** The tag name. */
    private String tagName = null;
   
    /** The parent. */
    private XmlNode parent = null;
   
    /** The attributes. */
    private Map<String, String> attributes = new LinkedHashMap<String, String>();
   
    /** The nodes. */
    private List<Object> nodes = new ArrayList<Object>();
   
    /**
     * Instantiates a new xml node.
     */
    public XmlNode()
    {
    }
   
    /**
     * Instantiates a new xml node.
     *
     * @param tagName the tag name
     */
    public XmlNode(String tagName)
    {
        this.tagName = tagName;
    }
   
    /**
     * Gets the attributes.
     *
     * @return the attributes
     */
    public Map<String, String> getAttributes()
    {
        return this.attributes;
    }
   
    /**
     * Sets the attributes.
     *
     * @param attributes the attributes
     */
    public void setAttributes(Map<String, String> attributes)
    {
        if (attributes != null)
        {
            this.attributes = attributes;
        }
    }
   
    /**
     * Gets the children.
     *
     * @return the children
     */
    public List<Object> getChildren()
    {
        return this.nodes;
    }
   
    /**
     * Gets the children by tag name.
     *
     * @param name the name
     * @return the children by tag name
     */
    public List<XmlNode> getChildrenByTagName(String name)
    {
        List<XmlNode> list = new ArrayList<XmlNode>();
        List<Object> ch = getChildren();
        for (int i = 0; i < ch.size(); ++i)
        {
            if ((ch.get(i).getClass() != XmlNode.class)
                    || (!(((XmlNode) ch.get(i)).getTagName().equals(name))))
            {
                continue;
            }
            list.add((XmlNode) ch.get(i));
        }
       
        return list;
    }
   
    /**
     * Gets the first.
     *
     * @param tag the tag
     * @return the first
     */
    public XmlNode getFirst(String tag)
    {
        List<XmlNode> tem = getChildrenByTagName(tag);
        if (tem.isEmpty())
        {
            return null;
        }
        return (tem.get(0));
    }
   
    /**
     * Gets the last.
     *
     * @param tag the tag
     * @return the last
     */
    public XmlNode getLast(String tag)
    {
        List<XmlNode> tem = getChildrenByTagName(tag);
        if (tem.isEmpty())
        {
            return null;
        }
        return (tem.get(tem.size() - 1));
    }
   
    /**
     * Gets the first.
     *
     * @return the first
     */
    public Object getFirst()
    {
        List<Object> list = getChildren();
        if (list.isEmpty())
        {
            return null;
        }
        return list.get(0);
    }
   
    /**
     * Gets the last.
     *
     * @return the last
     */
    public Object getLast()
    {
        List<Object> list = getChildren();
        if (list.isEmpty())
        {
            return null;
        }
        return list.get(list.size() - 1);
    }
   
    /**
     * Adds the node.
     *
     * @param obj the obj
     * @return the xml node
     */
    public XmlNode addNode(Object obj)
    {
        if (obj == null)
        {
            throw new IllegalArgumentException("addNode:node cant null");
        }
        this.nodes.add(obj);
        if (obj instanceof XmlNode)
        {
            ((XmlNode) obj).setParent(this);
        }
        return this;
    }
   
    /**
     * Sets the attribute.
     *
     * @param name the name
     * @param value the value
     * @return the xml node
     */
    public XmlNode setAttribute(String name, String value)
    {
        this.attributes.put(name, value);
        return this;
    }
   
    /**
     * Gets the parent.
     *
     * @return the parent
     */
    public XmlNode getParent()
    {
        return this.parent;
    }
   
    /**
     * Sets the parent.
     *
     * @param parent the new parent
     */
    void setParent(XmlNode parent)
    {
        this.parent = parent;
    }
   
    /**
     * Checks if is root.
     *
     * @return true, if is root
     */
    public boolean isRoot()
    {
        return (this.parent == null);
    }
   
    /**
     * Gets the tag name.
     *
     * @return the tag name
     */
    public String getTagName()
    {
        return this.tagName;
    }
   
    /**
     * Sets the tag name.
     *
     * @param tagName the tag name
     * @return the xml node
     */
    public XmlNode setTagName(String tagName)
    {
        this.tagName = tagName;
        return this;
    }
   
    public String getText()
    {
    if (getChildren().isEmpty())
    {
    return null;
    }
    if (getChildren().size() != 1)
    {
    throw new RuntimeException("Not leaf node,nodeName:" + getTagName());
    }
    return getChildren().get(0).toString();
    }
   
    public String getAttribute(String arrtibuteName)
    {
    return getAttributes().get(arrtibuteName);
    }
}
爱上
分享到:
评论

相关推荐

    跨平台解析XML文件 XmlNode V1.01测试版

    Mark: xml文件保存路径为已经设置的路径,默认为打开文件时的路径,也可以通过SetValue函数修改路径,详见SetValue函数说明 如果当前节点为根节点,则记录的xml文件路径将被替换 如果当前节点不是跟节点,只保存...

    复杂XML的解析及组装

    此包可以解决XML文件的解析、对象转化为XML字符串的问题。 1 通过调用解析类,可以将XML的DATA数据转换为XmlNode对象,XmlNode以树形结构进行XML的数据封装,使用的时候按照树形结构进行数据的获取。(如有问题请...

    【ASP.NET编程知识】asp.net简单生成XML文件的方法.docx

    * XML 文件的生成和解析 扩展阅读 * ASP.NET 操作 XML 技巧总结 * ASP.NET 文件操作技巧汇总 * ASP.NET Ajax 技巧总结专题 * ASP.NET 缓存操作技巧总结 * ASP.NET 中的 XmlDocument 类详解 * ASP.NET 实现TreeView...

    C# xml多个同名节点操作

    方便对xml中任意节(多个同名节点)点属性进行获取、设置。 /// /// 设置指定节点的属性 /// /// &lt;param name="xmlContent"&gt;xml结构 /// 父节点 /// 父节点所处同名节点的位置 为0表明只有一个该节点 /// ...

    根据XSD检查XML并修复

    附件是我写的一个demo程序,该程序的功能是根据一个定义好的XSD文件去检查...1 解析XSD文件,并将其规定的XML结构映射至自定义的Java类(XMLNode); 2 根据得到XML结构,逐一检查目标XML中是否存在指定的元素或属性。

    XML文件修改节点属性值(多种方法)

    xml 文件内容: 代码如下: &lt;?xml version=”1.0″ encoding=”utf-8″?&gt; &lt;subtitles&gt; &lt;info&gt; 最新通告:五一放假七天!请各教员悉知&lt;/content&gt; &lt;speed&gt;4&lt;/speed&gt; &lt;color&gt;red&lt;/color&gt; &lt;/info&gt; &lt;/subtitles&gt; C#...

    C#XML入门经典 C#编程人员必备的XML技能.part2

    XML概述 &lt;br&gt;2.1 XML的概念 2.1.1 XML元素 2.1.2 XML属性 2.1.3 XML解析器 2.1.4 构建XML 2.1.5 XML文档的各个组成部分 2.2 创建格式良好的XML文档 2.2.1 XML中的元素 2.2.2 XML中的属性...

    XML解析处理

    来自CodeProject的jerry.Wang 的xml操作类,简单方便,...xsl类添加了直接将xml和xsl进行转化得到转化后的html代码的函数,从而不用生成文件再得到转化结果。 xml的用法参见source文件夹的示例,原作者写的非常棒!

    C#实现影院售票系统

    1.首先是将解析完的XML文件绑定到TreeView上 2.用代码动态生成座位的label,生成触发事件Label_Click,俩组放映时间的售出座位是不同的 3.用序列化与反序列化实现代码的多次利用 4.打印票务 创建与本项目相关的10个...

    Spring.net框架

    public object Create(object parent, object configContext, System.Xml.XmlNode section) { ObjectInfo info; PropertyInfo propInfo; ConfigInfo cfgInfo = new ConfigInfo(); foreach(XmlNode node in ...

Global site tag (gtag.js) - Google Analytics