java中dom解析,Java在DOM解析中的规范化-它是如何工作的?

作为@JBNizet对更多技术用户的回答的扩展,这里是org.w3c.dom.Node接口com.sun.org.apache.xerces.internal.dom.ParentNode看上去,让你知道它是如何工作的。public void normalize() {

// No need to normalize if already normalized.

if (isNormalized()) {

return;

}

if (needsSyncChildren()) {

synchronizeChildren();

}

ChildNode kid;

for (kid = firstChild; kid != null; kid = kid.nextSibling) {

kid.normalize();

}

isNormalized(true);}

它递归遍历所有节点并调用kid.normalize()

中重写此机制。org.apache.xerces.dom.ElementImplpublic void normalize() {

// No need to normalize if already normalized.

if (isNormalized()) {

return;

}

if (needsSyncChildren()) {

synchronizeChildren();

}

ChildNode kid, next;

for (kid = firstChild; kid != null; kid = next) {

next = kid.nextSibling;

// If kid is a text node, we need to check for one of two

// conditions:

//   1) There is an adjacent text node

//   2) There is no adjacent text node, but kid is

//      an empty text node.

if ( kid.getNodeType() == Node.TEXT_NODE )

{

// If an adjacent text node, merge it with kid

if ( next!=null && next.getNodeType() == Node.TEXT_NODE )

{

((Text)kid).appendData(next.getNodeValue());

removeChild( next );

next = kid; // Don't advance; there might be another.

}

else

{

// If kid is empty, remove it

if ( kid.getNodeValue() == null || kid.getNodeValue().length() == 0 ) {

removeChild( kid );

}

}

}

// Otherwise it might be an Element, which is handled recursively

else if (kid.getNodeType() == Node.ELEMENT_NODE) {

kid.normalize();

}

}

// We must also normalize all of the attributes

if ( attributes!=null )

{

for( int i=0; i

{

Node attr = attributes.item(i);

attr.normalize();

}

}

// changed() will have occurred when the removeChild() was done,

// so does not have to be reissued.

isNormalized(true);

}

希望这能帮你节省点时间。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值