问题
Is it possible to parse an XML string and use it as a UI5 view?
I like to do something like this:
var sXML = `<mvc:View
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
controllerName="controller.App"
displayBlock="true"
>
<App>...</App>
</mvc:View>`;
var oView = sap.ui.view({
id: "idstart1",
view: sXML,
type: "XML"
});
回答1:
Sure, by defining the property viewContent
with the string in sap.ui.xmlview
, it's possible as the API reference states:
viewContent
[...] can hold a view description as XML string or as already parsed XML Document.
As of 1.56
The factory function sap.ui.xmlview
is deprecated by now. Please use sap.ui.core.mvc.XMLView.create
[API] instead with the string value assigned to the definition
setting.
sap.ui.getCore().attachInit(() => sap.ui.require([
"sap/ui/core/mvc/XMLView",
], XMLView => XMLView.create({
definition:`<mvc:View
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
height="100%"
>
<App>
<Page title="My String View">
<Title text="Hello, world!"/>
</Page>
</App>
</mvc:View>`,
}).then(view => view.placeAt("content"))));
<script>
window["sap-ui-config"] = {
libs: "sap.ui.core, sap.m",
preload: "async",
async: true,
theme: "sap_belize",
"xx-waitForTheme": true
};
</script>
<script id="sap-ui-bootstrap" src="https://ui5.sap.com/resources/sap-ui-core.js"></script>
<body id="content" class="sapUiBody sapUiSizeCompact"></body>
来源:https://stackoverflow.com/questions/44872228/parse-xml-view-from-string