Skip to Content

Access Data from XML Model

I have the following code using which I load the XML data. I want to access the data in my controller. However, when I try the getProperty method, it gives me null. Where am I going wrong?

XML File - https://www.w3schools.com/xml/books.xml

onInit: function () {
var oModel = new sap.ui.model.xml.XMLModel("https://www.w3schools.com/xml/books.xml");
console.log(oModel);
this.getView().setModel(oModel);
debugger;
var a = oModel.getProperty("/bookstore/book/title");
alert(a);
}

I am open to using any other method or way as well.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Sep 09, 2017 at 08:08 PM

    i believe you will get same origin issue at first line while model initialisation,

    check console (F12) if you are getting 'Access-Control-Allow-Origin' issue, you can use that url using proxy, and then try to get property n check.

    Add comment
    10|10000 characters needed characters exceeded

  • Sep 09, 2017 at 02:36 PM

    Use a json model.

    new sap.ui.model.json.JSONModel();

    You need to either manually copy the content or execute the service URI to get the JSON or XML data.

    By referring to an URI, it will not execute the service.

    Add comment
    10|10000 characters needed characters exceeded