• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ArkUI (JavaScript) Development
2
3## How do I convert the fields in an XML file into JavaScript objects?
4
5Applicable to: OpenHarmony SDK 3.2.3.5, stage model of API version 9
6
7To convert fields in an XML file into JavaScript objects, call the **convert** API in the **convertxml** module.
8
9Example:
10
11
12```
13import convertxml from '@ohos.convertxml';
14// XML strings
15let xml =
16  '<?xml version="1.0" encoding="utf-8"?>' +
17  '<note importance="high" logged="true">' +
18  '    <title>Happy</title>' +
19  '    <todo>Work</todo>' +
20  '    <todo>Play</todo>' +
21  '</note>';
22let conv = new convertxml.ConvertXML();
23// Options for conversion. For details, see the reference document.
24let options = {
25  trim: false,
26  declarationKey: "_declaration",
27  instructionKey: "_instruction",
28  attributesKey: "_attributes",
29  textKey: "_text",
30  cdataKey: "_cdata",
31  doctypeKey: "_doctype",
32  commentKey: "_comment",
33  parentKey: "_parent",
34  typeKey: "_type",
35  nameKey: "_name",
36  elementsKey: "_elements"
37}
38let result: any = conv.convert(xml, options) // Convert fields in the XML file into JavaScript objects.
39console.log('Test: ' + JSON.stringify(result))
40console.log('Test: ' + result._declaration._attributes.version) // version field in the XML file
41console.log('Test: ' + result._elements[0]._elements[0]._elements[0]._text) // title field in the XML file
42```
43
44For details, see [XML-to-JavaScript Conversion](../reference/apis/js-apis-convertxml.md).
45
46## How do I convert the time to the HHMMSS format?
47
48Example:
49
50
51```
52export default class DateTimeUtil{
53  /**
54  * HHMMSS
55  */
56  getTime() {
57    const DATETIME = new Date()
58    return this.concatTime(DATETIME.getHours(),DATETIME.getMinutes(),DATETIME.getSeconds())
59  }
60  /**
61  * YYYYMMDD
62  */
63  getDate() {
64    const DATETIME = new Date()
65    return this.concatDate(DATETIME.getFullYear(),DATETIME.getMonth()+1,DATETIME.getDate())
66  }
67  /**
68  * If the date is less than 10, add a leading zero, for example, **07**.
69  * @param value Indicates the value.
70  */
71  fill(value:number) {
72    return (value> 9 ? '' : '0') + value
73  }
74  /**
75  * Concatenate year, month, and date fields.
76  * @param year
77  * @param month
78  * @param date
79  */
80  concatDate(year: number, month: number, date: number){
81    return `${year}${this.fill(month)}${this.fill(date)}`
82  }
83  /**
84  Concatenate hours, minutes, and seconds fields.
85  * @param hours
86  * @param minutes
87  * @param seconds
88  */
89  concatTime(hours:number,minutes:number,seconds:number){
90    return `${this.fill(hours)}${this.fill(minutes)}${this.fill(seconds)}`
91  }
92}
93
94```
95