gpt4 book ai didi

java - 是否可以在父标签和子标签中写入 XMLSchema-instance?

转载 作者:行者123 更新时间:2023-12-01 11:29:31 26 4
gpt4 key购买 nike

我想创建以下 XML,其中在“ServiceAddRQ”以及“Service”标签中具有 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 。那么可以创建这样的 XML 吗?

必需的 XML:

<ServiceAddRQ echoToken="DummyEchoToken" version="2013/12"
xmlns="http://www.test.com/schemas/2005/06/messages"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com/schemas/2005/06/messages ServiceAddRQ.xsd">
<Service xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ServiceHotel" availToken="1jMqWGjqrx25Bp60gV2Qggb3">
</Service>
</ServiceAddRQ>

Java 生成的 XML: 在 java 生成的代码中,我得到“服务”标记的空白 xmlns...

<ServiceAddRQ xmlns="http://www.test.com/schemas/2005/06/messages"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" echoToken="6BB6B47EEEF4290515103925"
version="2013/12"
xsi:schemaLocation="http://www.test.com/schemas/2005/06/messages HotelValuedAvailRQ.xsd">
<Service xmlns="" xsi:type="ServiceHotel" availToken="1/YncBXZJXY17Z/ygNiO7gcg">
</Service>
</ServiceAddRQ>

相同的Java代码:

    static private Namespace NSSERVICE = Namespace.getNamespace("http://www.test.com/schemas/2005/06/messages");
static private Namespace NSSCHEMA = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

String sXMLOut = "";
Element barceloDS_requests= new Element("ServiceAddRQ",NSSERVICE);
barceloDS_requests.setAttribute("echoToken",searchParams.getSessionID().substring(0, 12)+GenTools.getSystemDateWithTime3());
barceloDS_requests.setAttribute("version","2013/12");
barceloDS_requests.addNamespaceDeclaration(NSSCHEMA);
barceloDS_requests.setAttribute("schemaLocation", "http://www.test.com/schemas/2005/06/messages HotelValuedAvailRQ.xsd", NSSCHEMA);

Namespace xsi = Namespace.getNamespace(GenTools.returnEmptyForNull(sXMLNS_XSI), "http://www.w3.org/2001/XMLSchema-instance");
Element eleService=new Element("Service");
eleService.setAttribute("type", "ServiceHotel", xsi);
eleService.setAttribute("availToken",contractInfo[1]);
barceloDS_requests.addContent(eleService);
sXMLOut = new GetXMLOutputter().getXMLOutputter(barceloDS_requests,true);

最佳答案

序列化程序添加 xmlns="" 是正确的,因为您创建了一个具有默认命名空间声明的顶级元素,但随后添加了一个不在命名空间中的子元素。要获得所需的输出,您需要使用与以下内容相同的 http://www.test.com/schemas/2005/06/messages 命名空间创建 Service 元素根ServiceAddRQ。尝试更像这样:

static private Namespace NSSERVICE = Namespace.getNamespace("http://www.test.com/schemas/2005/06/messages");
static private Namespace NSSCHEMA = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

String sXMLOut = "";
Element barceloDS_requests= new Element("ServiceAddRQ",NSSERVICE);
barceloDS_requests.setAttribute("echoToken",searchParams.getSessionID().substring(0, 12)+GenTools.getSystemDateWithTime3());
barceloDS_requests.setAttribute("version","2013/12");
barceloDS_requests.addNamespaceDeclaration(NSSCHEMA);
barceloDS_requests.setAttribute("schemaLocation", "http://www.test.com/schemas/2005/06/messages HotelValuedAvailRQ.xsd", NSSCHEMA);

// --------------
// Key change -- Create Service element in the right namespace
Element eleService=new Element("Service",NSSERVICE);
// --------------

eleService.setAttribute("type", "ServiceHotel", NSSCHEMA);
eleService.setAttribute("availToken",contractInfo[1]);
barceloDS_requests.addContent(eleService);

这将创建正确的输出:

<ServiceAddRQ echoToken="DummyEchoToken" version="2013/12"
xmlns="http://www.test.com/schemas/2005/06/messages"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.test.com/schemas/2005/06/messages ServiceAddRQ.xsd">
<Service xsi:type="ServiceHotel" availToken="1jMqWGjqrx25Bp60gV2Qggb3"/>
</ServiceAddRQ>

Service 元素不需要自己的 xmlns:xsi 声明,因为它是从父级继承的,但如果您特别想添加一个冗余的,那么它也许可以使用eleService.addNamespaceDeclaration(NSSCHEMA),但不能保证,因为当序列化器不改变结果的语义时,序列化器总是可以省略冗余的命名空间声明.

关于java - 是否可以在父标签和子标签中写入 XMLSchema-instance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30527714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com