Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以XML格式获取响应数据_Javascript_Xml_Sapui5 - Fatal编程技术网

Javascript 以XML格式获取响应数据

Javascript 以XML格式获取响应数据,javascript,xml,sapui5,Javascript,Xml,Sapui5,我们使用JSON.stringify(oData)以json格式获取数据,我们如何以xml格式获取数据 oModel.read('/', null, null, true, function(oData, oResponse){ var data = JSON.stringify(oData); document.write(data ); }); 要获取XML格式的响应,请将$format=XML添加到ODataURL中 见示例: ?$format=json ?$format

我们使用
JSON.stringify(oData)
以json格式获取数据,我们如何以xml格式获取数据

oModel.read('/', null, null, true, function(oData, oResponse){
    var data = JSON.stringify(oData);
    document.write(data );
});

要获取XML格式的响应,请将$format=XML添加到ODataURL中

见示例:

  • ?$format=json
  • ?$format=xml
  • 要获得XML格式的响应,请使用
    sap.ui.model.XML.XMLModel

    oModel.read('/?$format=xml', null, null, true, function(oData, oResponse){
        var xmlDataModel = new sap.ui.model.xml.XMLModel(oData);    
    });
    

    您使用的是OData服务,但您希望以某种格式获得响应。对我来说,这只意味着您希望绕过ODataModel机制为您所做的所有工作,获取OData服务器返回的原始XML。如果是这样的话,我有一个问题和一个答案给你

    问题:为什么

    答:如果您确实希望从OData响应中获取XML,即通常从Northwind entityset(例如)返回的原始Atom提要,那么您实际上可以在read()方法的成功回调中访问整个HTTP响应对象。稍微扩展一下您问题中的源代码,如下所示:

    oModel.read('/', null, null, true, function(oData, oResponse){
        document.write(oResponse.body);
    });
    
    <feed xml:base="http://services.odata.org/Northwind/Northwind.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <id>http://services.odata.org/Northwind/Northwind.svc/Products</id>
    <title type="text">Products</title>
    <updated>2014-08-26T07:33:36Z</updated>
    <link rel="self" title="Products" href="Products"/>
    <entry>
    <id>http://services.odata.org/Northwind/Northwind.svc/Products(1)</id>
    ...
    
    这将产生如下结果:

    oModel.read('/', null, null, true, function(oData, oResponse){
        document.write(oResponse.body);
    });
    
    <feed xml:base="http://services.odata.org/Northwind/Northwind.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <id>http://services.odata.org/Northwind/Northwind.svc/Products</id>
    <title type="text">Products</title>
    <updated>2014-08-26T07:33:36Z</updated>
    <link rel="self" title="Products" href="Products"/>
    <entry>
    <id>http://services.odata.org/Northwind/Northwind.svc/Products(1)</id>
    ...
    
    
    http://services.odata.org/Northwind/Northwind.svc/Products
    产品
    2014-08-26T07:33:36Z
    http://services.odata.org/Northwind/Northwind.svc/Products(1)
    ...
    

    但是-你确定你真的想要这个吗?

    当我放置var xmlDataModel=new sap.ui.model.xml.XMLModel(oData)时,我得到了这个“EventProvider sap.ui.model.xml.XMLModel”;不,我不明白。来自服务器的数据已经是xml。但我无法使用var xmlDataModel=new sap.ui.model.xml.XMLModel(oData)获取数据;