nsIINIParser 编辑

xpcom/ds/nsIINIParser.idlScriptable 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.

nsIUTF8StringEnumerator getKeys(in AUTF8String aSection);
nsIUTF8StringEnumerator getSections();
AUTF8String getString(in AUTF8String aSection, in AUTF8String aKey);

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:61 次

字数:6854

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文