java element node_Java OMNode.ELEMENT_NODE属性代码示例

public static Element toDOM(OMElement element, Document doc, boolean deepNS) {

final Element domElement = doc.createElementNS(element.getQName().getNamespaceURI(),

element.getQName().getLocalPart());

if (deepNS) {

NSContext nscontext = new NSContext();

buildNScontext(nscontext, element);

DOMUtils.injectNamespaces(domElement,nscontext);

} else {

if (element.getAllDeclaredNamespaces() != null) {

for (Iterator i = element.getAllDeclaredNamespaces(); i.hasNext(); ) {

OMNamespace omns = (OMNamespace)i.next();

if (omns.getPrefix().equals("")) {

domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns",

omns.getNamespaceURI() == null ? "" :

omns.getNamespaceURI());

}

else {

domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:"+ omns.getPrefix(),

omns.getNamespaceURI());

}

}

}

}

for (Iterator i = element.getAllAttributes(); i.hasNext();) {

final OMAttribute attr = (OMAttribute) i.next();

Attr newAttr;

if (attr.getNamespace() != null) {

newAttr = doc.createAttributeNS(attr.getNamespace().getNamespaceURI(),

attr.getLocalName());

} else {

newAttr = doc.createAttributeNS(null,attr.getLocalName());

}

newAttr.appendChild(doc.createTextNode(attr.getAttributeValue()));

domElement.setAttributeNodeNS(newAttr);

// Case of qualified attribute values, we're forced to add corresponding namespace declaration manually...

int colonIdx = attr.getAttributeValue().indexOf(":");

if (colonIdx > 0) {

OMNamespace attrValNs = element.findNamespaceURI(attr.getAttributeValue().

substring(0, colonIdx));

if(attrValNs!=null) {

domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:"+ attrValNs.getPrefix(),

attrValNs.getNamespaceURI());

}

}

}

for (Iterator i = element.getChildren(); i.hasNext();) {

OMNode omn = (OMNode) i.next();

switch (omn.getType()) {

case OMNode.CDATA_SECTION_NODE:

domElement.appendChild(doc.createCDATASection(((OMText)omn).getText()));

break;

case OMNode.TEXT_NODE:

domElement.appendChild(doc.createTextNode(((OMText)omn).getText()));

break;

case OMNode.ELEMENT_NODE:

domElement.appendChild(toDOM((OMElement)omn,doc, false));

break;

}

}

return domElement;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值