是否可以在父标记和子标记中编写XMLSchema实例?

时间:2015-05-29 10:53:19

标签: java xml jdom

我想创建以下XML,其中“ServiceAddRQ”中的 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生成的代码中的

Java生成的XML:我得到了“服务”标签的空白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);

1 个答案:

答案 0 :(得分:1)

序列化程序添加xmlns=""是正确的,因为您创建了一个带有默认命名空间声明的顶级元素,但随后添加了一个不在命名空间中的子元素。要获得所需的输出,您需要创建Service元素,其具有与根http://www.test.com/schemas/2005/06/messages相同的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)但不能保证,因为当序列化程序不更改结果的语义时,它总是可以省略冗余的名称空间声明。