我想在“ServiceAddRQ”以及“服务”标签中创建下面的XML,其中具有xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance”。那么可以创建这样的XML吗?是否有可能在父标签和子标签中编写XMLSchema实例?
所需的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);
对于“如何更改我的代码以生成X而不是Y”的问题,您确实需要向我们显示您想要更改的当前代码......但请注意,您实际上并不需要'xmlns: xsi'声明,因为“Service”元素会自动继承其父项的名称空间声明。 –
嗨@IanRoberts,感谢您的宝贵回应。你现在可以检查我的java代码片段。即使子标签继承父标签命名空间,为什么我在Service标签中获得空白'xmlns'? –
您可以确认您使用的是哪种XML对象模型,因为它绝对不是DOM(如在org.w3c.dom中)。它看起来像JDOM,对吗? –