I wish to read these values from my Cordova/PhoneGap application's config.xml
at runtime:
我希望在运行时从Cordova / PhoneGap应用程序的config.xml中读取这些值:
- name
- 名称
- copyright
- 版权
- description
- 描述
However, was surprised to see there is no 'Config
' feature in the API reference guide: http://cordova.apache.org/docs/en/3.4.0/index.html
然而,令人惊讶的是,API参考指南中没有“配置”功能:http://cordova.apache.org/docs/en/3.4.0/index.html
I've resorted to writing my own function that reads and parses this file manually, however I feel like there must be an (existing) better way.
我已经尝试编写自己的函数来手动读取和解析这个文件,但我觉得必须有一个(现有的)更好的方法。
Should developers be parsing config.xml manually to extract necessary info, or is there an existing plugin that can be used to do this?
开发人员是否应手动解析config.xml以提取必要的信息,或者是否有可用于执行此操作的现有插件?
3 个解决方案
#1
4
You could use following code on iOS, WP7, WP8, Windows8, and probably Ubuntu
你可以在iOS,WP7,WP8,Windows8和Ubuntu上使用以下代码
function readConfig() {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function () {
var parser = new DOMParser();
var doc = parser.parseFromString(xhr.responseText, "application/xml");
alert("Description : " +
doc.getElementsByTagName("description").item(0).textContent);
});
xhr.open("get", "../config.xml", true);
xhr.send();
}
for the Android you have to change path to file from "../config.xml"
to "../../android_res/xml/config.xml"
对于Android,您必须将文件路径从“../config.xml”更改为“../../android_res/xml/config.xml”
Taken from Cordova mailing where discussed answer: https://www.mail-archive.com/dev@cordova.apache.org/msg14313.html
取自Cordova邮件,其中讨论了答案:https://www.mail-archive.com/dev@cordova.apache.org/msg14313.html
Also there not-official plugin for reading configuration: https://github.com/apache/cordova-labs/tree/cdvtest/cordova-plugin-appsettings
还有用于阅读配置的非官方插件:https://github.com/apache/cordova-labs/tree/cdvtest/cordova-plugin-appsettings
#2
1
For the ones who don't want to mess with the xhr queries, there are two plugins you can use:
对于那些不想搞乱xhr查询的人,你可以使用两个插件:
1 plugin-buildinfo (just for Android and IOS but sooo great)
1 plugin-buildinfo(仅适用于Android和IOS,但非常棒)
2 plugin-app-version (lighter but supports more platforms)
2插件-app-version(更轻但支持更多平台)
To start quickly with the second one, all you need to do is adding the plugin into your project:
要快速启动第二个,您需要做的就是将插件添加到项目中:
cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.git
and calling where you want as follows:
并调用你想要的地方如下:
cordova.getAppVersion(function (version) {
alert(version);
});
#3
0
You could use following Cordova plugin:
您可以使用以下Cordova插件:
cordova plugin add cordova-plugin-customconfigparameters
Add your Custom Parameters in Config.xml
as preference tags:
在Config.xml中添加自定义参数作为首选项标记:
<preference name="name" value="Ibrahim"/>
<preference name="copyright" value="Direct Direction 2017"/>
<preference name="description" value="Information Technology"/>
Note: be sure the preference name should be a small letter (to work on IOS).
注意:确保首选项名称应为小写字母(适用于IOS)。
Then in your page Get a key's value from the Config.xml
using the following script:
然后在您的页面中使用以下脚本从Config.xml获取密钥的值:
var paramkeyArray=["name","copyright","description"];
CustomConfigParameters.get(function(configData){
console.log(configData.name);
console.log(configData.copyright);
console.log(configData.description);
},function(err){
console.log(err);
},paramkeyArray);
For more detail see https://www.npmjs.com/package/cordova-plugin-customconfigparameters
有关更多详细信息,请参阅https://www.npmjs.com/package/cordova-plugin-customconfigparameters
#1
4
You could use following code on iOS, WP7, WP8, Windows8, and probably Ubuntu
你可以在iOS,WP7,WP8,Windows8和Ubuntu上使用以下代码
function readConfig() {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function () {
var parser = new DOMParser();
var doc = parser.parseFromString(xhr.responseText, "application/xml");
alert("Description : " +
doc.getElementsByTagName("description").item(0).textContent);
});
xhr.open("get", "../config.xml", true);
xhr.send();
}
for the Android you have to change path to file from "../config.xml"
to "../../android_res/xml/config.xml"
对于Android,您必须将文件路径从“../config.xml”更改为“../../android_res/xml/config.xml”
Taken from Cordova mailing where discussed answer: https://www.mail-archive.com/dev@cordova.apache.org/msg14313.html
取自Cordova邮件,其中讨论了答案:https://www.mail-archive.com/dev@cordova.apache.org/msg14313.html
Also there not-official plugin for reading configuration: https://github.com/apache/cordova-labs/tree/cdvtest/cordova-plugin-appsettings
还有用于阅读配置的非官方插件:https://github.com/apache/cordova-labs/tree/cdvtest/cordova-plugin-appsettings
#2
1
For the ones who don't want to mess with the xhr queries, there are two plugins you can use:
对于那些不想搞乱xhr查询的人,你可以使用两个插件:
1 plugin-buildinfo (just for Android and IOS but sooo great)
1 plugin-buildinfo(仅适用于Android和IOS,但非常棒)
2 plugin-app-version (lighter but supports more platforms)
2插件-app-version(更轻但支持更多平台)
To start quickly with the second one, all you need to do is adding the plugin into your project:
要快速启动第二个,您需要做的就是将插件添加到项目中:
cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.git
and calling where you want as follows:
并调用你想要的地方如下:
cordova.getAppVersion(function (version) {
alert(version);
});
#3
0
You could use following Cordova plugin:
您可以使用以下Cordova插件:
cordova plugin add cordova-plugin-customconfigparameters
Add your Custom Parameters in Config.xml
as preference tags:
在Config.xml中添加自定义参数作为首选项标记:
<preference name="name" value="Ibrahim"/>
<preference name="copyright" value="Direct Direction 2017"/>
<preference name="description" value="Information Technology"/>
Note: be sure the preference name should be a small letter (to work on IOS).
注意:确保首选项名称应为小写字母(适用于IOS)。
Then in your page Get a key's value from the Config.xml
using the following script:
然后在您的页面中使用以下脚本从Config.xml获取密钥的值:
var paramkeyArray=["name","copyright","description"];
CustomConfigParameters.get(function(configData){
console.log(configData.name);
console.log(configData.copyright);
console.log(configData.description);
},function(err){
console.log(err);
},paramkeyArray);
For more detail see https://www.npmjs.com/package/cordova-plugin-customconfigparameters
有关更多详细信息,请参阅https://www.npmjs.com/package/cordova-plugin-customconfigparameters