nsIINIParser 编辑
xpcom/ds/nsIINIParser.idl
Scriptable An instance of nsIINIParser
can be used to read values from an INI file. Inherits from: nsISupports
Last changed in Gecko 1.8 (Firefox 1.5 / Thunderbird 1.5 / SeaMonkey 1.0)Typically, you'll create an nsIINIParser
object by calling nsIINIParserFactory.createINIParser()
. Also, if you need to write to an INI file, use nsIINIParserWriter
.
|
|
|
Methods
getKeys()
Returns an nsIUTF8StringEnumerator
providing the keys available within the specified section of the INI file.
nsIUTF8StringEnumerator getKeys( in AUTF8String aSection );
Parameters
aSection
- The name of the section whose keys you wish to enumerate.
Return value
An nsIUTF8StringEnumerator
object that can be used to access the section's keys.
getSections()
Returns an nsIUTF8StringEnumerator
providing a list of the sections available within the INI file.
nsIUTF8StringEnumerator getSections();
Parameters
None.
Return value
An nsIUTF8StringEnumerator
object that can be used to access the sections in the INI file.
getString()
Returns the string value for the specified key within a particular section of the INI file.
AUTF8String getString( in AUTF8String aSection, in AUTF8String aKey );
Parameters
aSection
- The section containing the key whose value is to be returned.
aKey
- The key for which the value should be returned.
Return value
The string value of the specified key.
Examples
Getting a value for a key
This example provides a function you can call to obtain the value for a specific key in a given INI file.
/** * Returns the value for a given property in an INI file * @param {nsIFile} iniFile * The ini file to get the value from * @param {String} section * The name of the section in the ini file. INI sections are * defined by square brakets and look like this: [Settings] * All entries below such a section definition belong to that * section (until the next section). * @param {String} prop * The name of the property to get. */ function getIniValue (iniFile, section, prop) { var iniFact = Components.manager.getClassObjectByContractID( "@mozilla.org/xpcom/ini-parser-factory;1", Components.interfaces.nsIINIParserFactory ); var iniParser = iniFact.createINIParser(iniFile); try { return iniParser.getString(section,prop); } catch(e) { return undefined; } } // Usage: var lang = getIniValue(file,"Setting","Language");
Enumerating sections
This example gets a list of all the sections in the INI file.
// get all sections in the ini file var sectEnum = iniParser.getSections(); // save the sections in an array var sections = []; while (sectEnum && sectEnum.hasMore()) { var section = sectEnum.getNext(); // add an array entry for each section sections.push(section); }
Enumerating keys
This example shows how to fetch the values for all the keys in the section named "Setting".
// get all keys for the "Setting" section var keysEnum = iniParser.getKeys("Setting"); // save the keys and values in an object var keys = {}; while (keysEnum && keysEnum.hasMore()) { var key = keysEnum.getNext(); // create a property for each key and assing the ini value. keys[key] = iniParser.getString("Setting", key); }
See Also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论