# dd-plist **Repository Path**: zhangjun93/dd-plist ## Basic Information - **Project Name**: dd-plist - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 16 - **Created**: 2022-06-07 - **Last Updated**: 2024-06-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # dd-plist ## 简介 > 解析生成属性列表文件的工具库 ![动画](动画.gif) ## 下载安装 ```shell npm install @ohos/dd-plist --save ``` OpenHarmony npm环境配置等更多内容,请参考 [如何安装OpenHarmony npm包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_npm_usage.md) 。 ## 使用说明 1.在page页面中引入属性列表库必要的文件 ``` import PropertyListParser from '../plist/PropertyListParser.ets'; ``` 2.使用 ``` - 解析xml格式的属性列表文件 let _this = this resourceManager.getResourceManager((error, mgr) => { mgr.getRawFile("xmlArrayForParse.plist", (error, value) => { if (error != null) { console.error("cannot find file! e: " + error); } else { let arr: Int8Array = ArrayUtils.uint8Arr2Int8Arr(value) PropertyListParser.parseByBytes(arr, (obj: NSObject) => { if (obj instanceof NSArray) { let nsa: NSArray = obj _this.clear() _this.parsePlistText = "IntegerValue = " + nsa.getArray().toString() } }) } }); }); - 解析二进制或ASCII码格式的属性列表文件 let _this = this resourceManager.getResourceManager((error, mgr) => { mgr.getRawFile("asciiForParse.plist", (error, value) => { if (error != null) { console.error("cannot find file! e: " + error); } else { let arr: Int8Array = ArrayUtils.uint8Arr2Int8Arr(value) let root = PropertyListParser.parseByBytes(arr) _this.clear() _this.parsePlistText = root.toXMLPropertyList() } }); }); - 转换为XML格式字符串 let root: NSDictionary = new NSDictionary(); let people: NSArray = new NSArray(2); let person1: NSDictionary = new NSDictionary(); person1.putByObject("Name", "Peter"); //This will become a NSString let date = new Date() date.setFullYear(2011, 1, 13) date.setHours(9, 28) person1.putByObject("RegistrationDate", new NSDate(null, null, null, null, date)); //This will become a NSDate person1.putByObject("Age", 23); //This will become a NSNumber let person2: NSDictionary = new NSDictionary(); person2.putByObject("Name", "Lisa"); person2.putByObject("Age", 42); person2.putByObject("RegistrationDate", new NSDate(null, null, null, "2010-09-23T12:32:42Z")); people.setValue(0, person1); people.setValue(1, person2); root.put("People", people); this.parsePlistText = root.toXMLPropertyList() - 生成XML格式文件 let path = 'data/local/tmp/com.example.dd_plist/People.plist' fileio.createStream(path, "w+",function(err, stream){ if (err != null) { console.log("e:" + err) } else { PropertyListParser.saveAsXMLByStream(root, stream) stream.closeSync(); } }); ``` ## 接口说明 1. 通过文件对象解析plist文件 `PropertyListParser.parse(File f)` 2. 通过文件路径解析plist文件 `PropertyListParser.parse(String filePath)` 3. 通过字节数组解析plist文件 `PropertyListParser.parse(byte[] bytes)` 4. 通过文件流解析plist文件 `PropertyListParser.parse(InputStream is)` 5. 通过NSObject对象生成一个XML格式的plist文件 `PropertyListParser.saveAsXML(NSObject root, File out)` 6. 通过NSObject对象生成一个XML格式的plist文件流 `PropertyListParser.saveAsXML(NSObject root, OutputStream out)` 7. 将一个其他格式的plist文件转换为一个XML格式的plist文件 `PropertyListParser.convertToXml(File in, File out)` ## 兼容性 支持 OpenHarmony API version 8 及以上版本。 ## 目录结构 ```` |---- dd-plist | |---- entry # 示例代码文件夹 | |---- dd_plist # 库文件夹 |----src |----main |----ets |----components |----plist 存放解析库的业务逻辑代码 |----utils 解析库所用到的工具类 | |---- index.ets # 对外接口 | |---- README.md # 安装使用方法 ```` ## 贡献代码 使用过程中发现任何问题都可以提 [Issue](https://gitee.com/openharmony-sig/dd-plist/issues) 给我们,当然,我们也非常欢迎你给我们发 [PR](https://gitee.com/openharmony-sig/dd-plist/pulls) 。 ## 开源协议 本项目基于 [MIT License](https://gitee.com/openharmony-sig/dd-plist/blob/master/LICENSE) ,请自由地享受和参与开源。