繁体   English   中英

如何在Java Compute Node中设置xmlns属性值?

[英]How to set xmlns attribute value in Java Compute Node?

我试图将以下xml消息传播到IBM Integration Bus中的文件输出节点。

<Resto xmlns = 'https://stackoverflow.com'><Location network_id="5dfweg68h"><Category>Continental</Category></Location></Resto>

以下是我相关的Java代码来构造上述xml消息:

MbElement xmlBody = outMessage.getRootElement().createElementAsLastChild(MbXMLNSC.PARSER_NAME);
MbElement xmlParent = xmlBody.createElementAsLastChild(MbElement.TYPE_NAME, "Resto", null);
xmlParent.createElementAsLastChild(MbXMLNSC.ATTRIBUTE, "xmlns", "https://stackoverflow.com");

MbElement locationParser = xmlParent.createElementAsLastChild(MbElement.TYPE_NAME, "Location", null);
locationParser.createElementAsLastChild(MbXMLNSC.ATTRIBUTE, "network_id", "5dfweg68h");
locationParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"Category","Continental");

在调试我的应用程序时,我遇到了XML写入错误。 我仔细考虑了必要的解决方案,发现添加名称空间前缀可以解决此问题。 没有前缀就不能做到吗? 如果否,那么有人可以通过建议在Java计算节点中进行必要的更改来指导我分配必要的值吗?

注意:分配给xmlns的字符串应在xml消息中的单引号内。

使用常量MbXMLNSC.NAMESPACE_DECLARATION声明名称空间:

MbElement xmlBody = outMessage.getRootElement().createElementAsLastChild(MbXMLNSC.PARSER_NAME);
MbElement xmlParent = xmlBody.createElementAsLastChild(MbElement.TYPE_NAME, "Resto", null);
xmlParent.createElementAsFirstChild(MbXMLNSC.NAMESPACE_DECLARATION, "xmlns", "https://stackoverflow.com");

如果您的XSD定义XML元素合格 ,则必须显式设置名称空间。 例子:

xmlParent.setNamespace("https://stackoverflow.com");
locationParser.setNamespace("https://stackoverflow.com");
locationParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,
     "Category", "Continental").setNamespace("https://stackoverflow.com");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM