Java substitution to_Java XmlObject.substitute方法代码示例

import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类

/**

* Qualifies a valid member of a substitution group. This method tries to use the

* built-in {@link XmlObject#substitute(QName, SchemaType)} and if succesful returns

* a valid substitution which is usable (not disconnected). If it fails, it uses

* low-level {@link XmlCursor} manipulation to qualify the substitution group. Note

* that if the latter is the case the resulting document is disconnected and should

* no longer be manipulated. Thus, use it as a final step after all markup is included.

*

* If newType is null, this method will skip {@link XmlObject#substitute(QName, SchemaType)}

* and directly use {@link XmlCursor}. This can be used, if you are sure that the substitute

* is not in the list of (pre-compiled) valid substitutions (this is the case if a schema

* uses another schema's type as a base for elements. E.g. om:Observation uses gml:_Feature

* as the base type).

*

* @param xobj

* the abstract element

* @param newInstance

* the new {@link QName} of the instance

* @param newType the new schemaType. if null, cursors will be used and the resulting object

* will be disconnected.

* @return if successful applied {@link XmlObject#substitute(QName, SchemaType)} a living object with a

* type == newType is returned. Otherwise null is returned as you can no longer manipulate the object.

*/

public static XmlObject qualifySubstitutionGroup(XmlObject xobj, QName newInstance, SchemaType newType) {

XmlObjectsubstitute = null;

if (newType != null) {

substitute = xobj.substitute(newInstance, newType);

if (substitute != null && substitute.schemaType() == newType

&& substitute.getDomNode().getLocalName().equals(newInstance.getLocalPart()))

{

return substitute;

}

}

XmlCursor cursor = xobj.newCursor();

cursor.setName(newInstance);

QName qName = new QName("http://www.w3.org/2001/XMLSchema-instance", "type");

cursor.removeAttribute(qName);

cursor.toNextToken();

if (cursor.isNamespace()) {

cursor.removeXml();

}

cursor.dispose();

return null;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值