Dom2Map--Util

public class Dom2Map {



public static final String VERSION = "0.2.2";

final static int OTHERH_NODE = 0;
final static int ELEMENT_NODE = Node.ELEMENT_NODE;
final static int TEXT_NODE = Node.TEXT_NODE;


final static String TAG_TEXT_NODE = "__TEXT_NODE__";
final static String TAG_OTHER_NODE = "_OTERH_NODE__";

int mType = OTHERH_NODE;
String mTag = null;
String mValue = null;
int mSize = 0;


HashMap<String, ArrayList<Dom2Map>> mNamedChildren = null;
ArrayList<Dom2Map> mGroup = null;


ArrayList<Dom2Map> mChildren = null;


HashMap<String, String> mAttrs = null;

public Dom2Map() {
}


public Dom2Map(Node node) {
if (node != null) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
mType = ELEMENT_NODE;
mTag = node.getNodeName();
NamedNodeMap attrs = node.getAttributes();
int length = attrs.getLength();
if( length > 0 ){
if (mAttrs == null) {
mAttrs = new HashMap<String, String>();
}
}
for (int i = 0; i < length; i++) {
Node attr = attrs.item(i);
mAttrs.put(attr.getNodeName(), attr.getNodeValue());
}
} else if (node.getNodeType() == Node.TEXT_NODE
|| node.getNodeType() == Node.CDATA_SECTION_NODE) {
mType = TEXT_NODE;
mValue = node.getNodeValue();
} else {
mTag = TAG_OTHER_NODE;
mValue = "";
}
}
}


public boolean validated(){
return mTag != null;
}

public int getType() {
return mType;
}


public String getTag() {
if (mType == ELEMENT_NODE) {
return mTag;
} else if (mType == TEXT_NODE) {
return TAG_TEXT_NODE;
} else {
return TAG_OTHER_NODE;
}
}


public int getSize() {
return mSize;
}


public ArrayList<Dom2Map> getGroup() {
return mGroup;
}


public void add(Dom2Map element) {


String tag = element.getTag();
if (mNamedChildren == null) {
mNamedChildren = new HashMap<String, ArrayList<Dom2Map>>();
}
ArrayList<Dom2Map> c = mNamedChildren.get(tag);


if (c == null) {
c = new ArrayList<Dom2Map>();
mNamedChildren.put(tag, c);
}
if (mChildren == null) {
mChildren = new ArrayList<Dom2Map>();
}


element.mGroup = c;
c.add(element);

mChildren.add(element);
mSize++;
}


public String value() {
if (mType == ELEMENT_NODE) {
if (mValue == null) {
for (int i = 0; i < mSize; i++) {
Dom2Map child = mChildren.get(i);
if ( child != null && child.getType() == Dom2Map.TEXT_NODE ) {
if (mValue == null)
mValue = child.value();
else
mValue += child.value();
}
}
}
}
if (mValue == null)
return "";
else
return mValue;
}


public String attr(String tag) {
String content = "";
if (mAttrs != null) {
String attr = mAttrs.get(tag);
if (attr != null)
content = attr;
}
return content;
}


public int size() {
return mSize;
}


public Dom2Map get(String tag) {
if (mNamedChildren != null) {
ArrayList<Dom2Map> c = mNamedChildren.get(tag);
if (c != null && c.size() > 0) {
return c.get(0);
}
}
return new Dom2Map();
}

public static void parse_traval(Node node, Dom2Map parent) {
if (node == null)
return;
if (node.getNodeType() == Node.ELEMENT_NODE) {
Dom2Map n1 = new Dom2Map(node);
parent.add(n1);


Node child = node.getFirstChild();
while (child != null) {
parse_traval(child,n1);
try{
child = child.getNextSibling();
}catch(IndexOutOfBoundsException e){ // android bug? http://code.google.com/p/android/issues/detail?id=779
child = null;
}
}
} else if (node.getNodeType() == Node.TEXT_NODE
|| node.getNodeType() == Node.CDATA_SECTION_NODE) {
Dom2Map n1 = new Dom2Map(node);
parent.add(n1);
}
}

public static Dom2Map parse(Node node){
Dom2Map root = new Dom2Map();
Node child = null;
if( node.getNodeType() != Node.ELEMENT_NODE){
child = node.getFirstChild();
while(child != null){
if( child.getNodeType() == Node.ELEMENT_NODE){
break;
}
child = child.getNextSibling();
}
}
if(child != null)
parse_traval(child,root);

return root;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值