1# js_api_module Subsystem/Component 2 3- [Introduction](#Introduction) 4- [Contents](#Contents) 5- [Illustrate](#Illustrate) 6 - [Interface Description](#Interface Description) 7 - [Instructions for use](#Instructions for use) 8 9- [Related warehouse](#Related warehouse) 10 11## Introduction 12 13The interface of URL is used to parse, construct, normalize, and encode URLs. The constructor of URL creates a new URL object. In order to make changes to the resolved components of the URL or to the URL. The URLSearchParams interface defines some practical methods to process URL query strings. 14 15URI Represents a Uniform Resource Identifier (URI) reference. 16 17XML representation refers to extensible markup language. 18 19The XmlSerializer interface is used to generate an xml file. The constructor of XmlSerializer creates a new XmlSerializer object, and calls the method of the XmlSerializer object to generate an xml file. The XmlPullParser interface is used to parse the existing xml file. The constructor of XmlPullParser creates a new XmlPullParser object, and calls the method of the XmlPullParser object to parse the xml file. 20 21## Contents 22 23``` 24base/compileruntime/js_api_module/ 25├── Class:URL # URL class 26│ ├── new URL(input[, base]) # Create URL object 27│ ├── hash # hash attribute 28│ ├── host # host attribute 29│ ├── hostname # hostname attribute 30│ ├── href # href attribute 31│ ├── origin # origin attribute 32│ ├── password # password attribute 33│ ├── pathname # pathname attribute 34│ ├── port # port attribute 35│ ├── protocol # protocol attribute 36│ ├── search # search attribute 37│ ├── searchParams # searchParams attribute 38│ ├── username # username attribute 39│ ├── toString() # toString method 40│ └── toJSON() # toJSON method 41├── Class: URLSearchParams # URLSearchParams class 42│ ├── new URLSearchParams() # Create URLSearchParams object 43│ ├── new URLSearchParams(string) # Create URLSearchParams object 44│ ├── new URLSearchParams(obj) # Create URLSearchParams object 45│ ├── new URLSearchParams(iterable) # Create URLSearchParams object 46│ ├── append(name, value) # append method 47│ ├── delete(name) # delete method 48│ ├── entries() # entries method 49│ ├── forEach(fn[, thisArg]) # forEach method 50│ ├── get(name) # get method 51│ ├── getAll(name) # getAll method 52│ ├── has(name) # has method 53│ ├── keys() # keys method 54│ ├── set(name, value) # set method 55│ ├── sort() # sort method 56│ ├── toString() # toString method 57│ ├── values() # values method 58│ └── urlSearchParams[Symbol.iterator]() # Create URLSearchParams object 59├── Class:URI # URI class 60│ ├── URI(str: string) # Create URI object 61│ ├── scheme # scheme attribute 62│ ├── authority # authority attribute 63│ ├── ssp # ssp attribute 64│ ├── userinfo # userinfo attribute 65│ ├── host # host attribute 66│ ├── port # port attribute 67│ ├── query # query attribute 68│ ├── fragment # fragment attribute 69│ ├── path # path method 70│ ├── equals(ob: Object) # equals method 71│ ├── normalize() # normalize method 72│ ├── checkIsAbsolute() # checkIsAbsolute method 73│ ├── normalize() # normalize method 74│ ├── toString() # toString method 75├── Class:ConvertXml # ConvertXml class 76│ ├── ConvertXml() # Create convertxml class object 77│ └── convert(xml: string, options: Object) # convert method 78├── Class:XmlSerializer # XmlSerializer class 79│ ├── new XmlSerializer(buffer: ArrayBuffer | DataView, encoding?: string) # Create XmlSerializer class object 80│ ├── setAttributes(name: string, value: string) # Set Attributes method 81│ ├── addEmptyElement(name: string) # Add an empty element method 82│ ├── setDeclaration() # Set the Declaration method 83│ ├── startElement(name: string) # Set the start element method 84│ ├── endElement() # Set the end element method 85│ ├── setNamespace(prefix: string, namespace: string) # Namespace method 86│ ├── setCommnet(text: string) # Set up Commnet method 87│ ├── setCData(text: string) # Set the CData method 88│ ├── setText(text: string) # Set the Text method 89│ ├── setDocType(text: string) # Set DocType method 90└── Class: XmlPullParser # XmlPullParser class 91 ├── new (buffer: ArrayBuffer | DataView, encoding?: string) # Create XmlPullParser object 92 └── parse(option: ParseOptions) # parse method 93``` 94 95## Illustrate 96 97### Interface Description 98 99 100| Interface name | Illustrate | 101| -------- | -------- | 102| new URL(url: string,base?:string \| URL) | Create and return a URL object that references the URL specified by the absolute URL string, the relative URL string, and the basic URL string. | 103| tostring():string | The stringification method returns a USVString containing the complete URL. It is equivalent to the read-only URL.href. | 104| toJSON():string | This method returns a USVString, which contains a serialized URL version. | 105| new URLSearchParams() | The URLSearchParams() constructor has no parameters. This method creates and returns a new URLSearchParams object. The beginning'?' character will be ignored. | 106| new URLSearchParams(string) | The input parameter of URLSearchParams(string) constructor is string data type. This method creates and returns a new URLSearchParams object. The beginning'?' character will be ignored. | 107| new URLSearchParams(obj) | URLSearchParams(obj) The input parameter of the constructor is the obj data type. This method creates and returns a new URLSearchParams object. The beginning'?' character will be ignored. | 108| new URLSearchParams(iterable) | URLSearchParams(iterable) The input parameter of the constructor is the iterable data type. This method creates and returns a new URLSearchParams object. The beginning'?' character will be ignored. | 109| has(name: string): boolean | Retrieve whether the searchParams object contains name. If yes, it returns true, otherwise it returns false. | 110| set(name: string, value string): void | Retrieve whether the searchParams object contains a key-value pair whose key is name. If not, add the key-value pair, if any, modify the value corresponding to the first key in the object, and delete the remaining key-value pairs whose key is name. | 111| sort(): void | According to the Unicode code point of the key, sort all key/value pairs contained in this object and return undefined. | 112| toString(): string | According to the searchParams object, the query string applicable in the URL is returned. | 113| keys(): iterableIterator\<string> | Return an iterator, which allows iterating through all the key values contained in the object. | 114| values(): iterableIterator\<string> | Returns an iterator, which allows iterating over all the value values contained in the object. | 115| append(name: string, value: string): void | Insert the name, value key-value pair in the searchParams object. | 116| delete(name: string): void | Traverse the searchParams object, find all the names, and delete the corresponding key-value pairs. | 117| get(name: string): string | Retrieve the first name in the searchParams object and return the value corresponding to the name key. | 118| getAll(name: string): string[] | Retrieve all names in the searchParams object and return all the values corresponding to the name key. | 119| entries(): iterableIterator<[string, string]> | Returns an iterator that allows iterating through all key/value pairs contained in the searchParams object. | 120| forEach(): void | Through the callback function to traverse the key-value pairs on the URLSearchParams instance object. | 121| urlSearchParams\[Symbol.iterator]() | Returns an ES6 iterator for each name-value pair in the query string. Each item of the iterator is a JavaScript array. | 122| URI(str: string) | Construct the URI by parsing the given input parameter (String str). This constructor parses the given string strictly in accordance with the grammatical provisions in RFC 2396 Appendix A. | 123| getScheme() | Return the scheme component of this URI, or null if the scheme is not defined. | 124| getAuthority() | Returns the decoded authority component of this URI, or null if authority is not defined. The string returned by this method is the same as the string returned by the getRawAuthority method, except that all escaped octet sequences are decoded. | 125| getSchemeSpecificPart() | Returns the decoding scheme-specific part of this URI. The string returned by this method is the same as the string returned by the getRawSchemeSpecificPart method, except that all escaped octet sequences are decoded. | 126| getUserInfo() | Returns the decoded userinfo component of this URI. The userinfo component of the URI (if defined) only contains characters in unreserved, punctuation, escape, and other categories. | 127| getHost() | Return the host component of this URI, or null if host is not defined. | 128| getPort() | Return the port of this URI, or -1 if the port is not defined. The port component of the URI (if defined) is a non-negative integer. | 129| getQuery() | Returns the decoded query component of this URI, or null if the query is not defined. The string returned by this method is the same as the string returned by the getRawQuery method, except that all escaped octet sequences are decoded. | 130| getFragment() | Returns the decoded fragment component of this URI, or null if the fragment is not defined. The string returned by this method is the same as the string returned by the getRawFragment method, except that all escaped octet sequences are decoded. | 131| getPath() | Returns the decoded path component of this URI, or null if path is not defined. The string returned by this method is the same as the string returned by the getRawPath method, except that all escaped octet sequences are decoded. | 132| equals(ob: Object) | Test whether this URI is equal to another object. If the given object is not a URI, this method immediately returns false. | 133| normalize() | Normalize the path of this URI. If this URI is opaque, or its path is already in normal form, then this URI is returned. Otherwise, a new URI identical to this URI will be constructed. | 134| checkIsAbsolute() | Determine whether this URI is absolute. If and only if it has a scheme component, the URI is absolute and the return value is true, otherwise the return value is false. | 135| toString() | Return the content of this URI as a string. | 136| ConvertXml() | The constructor used to construct the convertxml class object. This constructor does not need to pass in parameters. | 137| convert(xml: string, options: Object) | Returns a JavaScript object that converts an XML string as required by the option. | 138| XmlSerializer(buffer: ArrayBuffer \| DataView, encoding?: string) | Create and return an XmlSerializer object. The XmlSerializer object passes two parameters. The first parameter is ArrayBuffer or DataView, and the second parameter is the file format (UTF-8 by default). | 139| setAttributes(name: string, value: string): void | Write the Attributes attribute to the xml file. | 140| addEmptyElement(name: string): void | Write an empty element. | 141| setDeclaration(): void | Set the Declaration to use the encoding to write the xml declaration. For example: <? xml version="1.0" encoding="utf-8"> | 142| startElement(name: string): void | Write the elemnet start tag with the given name.| 143| endElement(): void | Write the end tag of the element. | 144| setNamespace(prefix: string, namespace: string): void | Write the namespace of the current element tag. | 145| setCommnet(text: string): void | Write the comment attribute. | 146| setCData(text: string): void | Write the CData attribute. | 147| setText(text: string): void | Write the Text property. | 148| setDocType(text: string): void | Write the DocType attribute. | 149| XmlPullParser(buffer: ArrayBuffer \| DataView, encoding?: string) | Create and return an XmlPullParser object. The XmlPullParser object passes two parameters. The first parameter is ArrayBuffer or DataView, and the second parameter is the file format (default is UTF-8). | 150| parse(option: ParseOptions): void | This interface is used to parse xml. The ParseOptions parameter is an interface containing five optional parameters {supportDoctype?: boolea ignoreNameSpace?: boolean tagValueCallbackFunction?: (name: string, value: string) => boolean attributeValueCallbackFunction?: (name: string, value: string) => boolean) tokenValueCallbackFunction?: (eventType: EventType, value: ParseInfo) => boolean }. The input parameter 1 of the tokenValueCallbackFunction callback function is the event type, and the input parameter 2 is the info interface containing get attributes such as getColumnNumber and getDepth. The user can obtain the depth and other information in the current parsing process through methods such as info.getDepth(). | 151### Instructions for use 152 153The usage of each interface is as follows: 154 155 1561、new URL(url: string,base?:string|URL) 157``` 158let b = new URL('https://developer.mozilla.org'); // => 'https://developer.mozilla.org/' 159 160let a = new URL( 'sca/./path/path/../scasa/text', 'http://www.example.com'); 161// => 'http://www.example.com/sca/path/scasa/text' 162``` 1632、tostring():string 164``` 165const url = new URL('http://10.0xFF.O400.235:8080/directory/file?query#fragment'); 166url.toString() // => 'http://10.0xff.o400.235:8080/directory/file?query#fragment' 167 168const url = new URL("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html"); 169url.toString() // => 'http://[fedc:ba98:7654:3210:fedc:ba98:7654:3210]/index.html' 170 171const url = new URL("http://username:password@host:8080/directory/file?query#fragment"); 172url.toString() // => 'http://username:password@host:8080/directory/file?query#fragment' 173``` 1743、toJSON():string 175``` 176const url = new URL("https://developer.mozilla.org/en-US/docs/Web/API/URL/toString"); 177url.toJSON(); // => 'https://developer.mozilla.org/en-US/docs/Web/API/URL/toString' 178``` 1794、new URLSearchParams() 180``` 181let params = new URLSearchParams('foo=1&bar=2'); 182``` 1835、new URLSearchParams(string) 184``` 185params = new URLSearchParams('user=abc&query=xyz'); 186console.log(params.get('user')); 187// Prints 'abc' 188``` 1896、new URLSearchParams(obj) 190``` 191const params = new URLSearchParams({ 192 user: 'abc', 193 query: ['first', 'second'] 194}); 195console.log(params.getAll('query')); 196// Prints [ 'first,second' ] 197``` 1987、new URLSearchParams(iterable) 199``` 200let params; 201 202// Using an array 203params = new URLSearchParams([ 204 ['user', 'abc'], 205 ['query', 'first'], 206 ['query', 'second'], 207]); 208console.log(params.toString()); 209// Prints 'user = abc & query = first&query = second' 210``` 2118、has(name: string): boolean 212``` 213console.log(params.has('bar')); // =>true 214``` 2159、set(name: string, value string): void 216``` 217params.set('baz', 3); 218``` 21910、sort(): void 220``` 221params .sort(); 222``` 22311、toString(): string 224``` 225console.log(params .toString()); // =>bar=2&baz=3&foo=1' 226``` 22712、keys(): iterableIterator\<string> 228``` 229for(var key of params.keys()) { 230 console.log(key); 231} // =>bar baz foo 232``` 23313、values(): iterableIterator\<string> 234``` 235for(var value of params.values()) { 236 console.log(value); 237} // =>2 3 1 238``` 23914、append(name: string, value: string): void 240``` 241params.append('foo', 3); // =>bar=2&baz=3&foo=1&foo=3 242``` 24315、delete(name: string): void 244``` 245params.delete('baz'); // => bar=2&foo=1&foo=3 246``` 24716、get(name: string): string 248``` 249params.get('foo'); // => 1 250``` 25117、getAll(name: string): string[] 252``` 253params.getAll('foo'); // =>[ '1', '3' ] 254``` 25518、entries(): iterableIterator<[string, string]> 256``` 257for(var pair of searchParams.entries()) { 258 console.log(pair[0]+ ', '+ pair[1]); 259} // => bar, 2 foo, 1 foo, 3 260``` 26119、forEach(): void 262``` 263url.searchParams.forEach((value, name, searchParams) => { 264 console.log(name, value, url.searchParams === searchParams); 265}); 266// => foo 1 true 267// => bar 2 true 268``` 26920、urlSearchParams[Symbol.iterator] () 270``` 271const params = new URLSearchParams('foo=bar&xyz=baz'); 272for (const [name, value] of params) { 273 console.log(name, value); 274} 275// Prints: 276// foo bar 277// xyz bar 278``` 279 280 28121、URI(String str) 282``` 283let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 284``` 28522、scheme 286``` 287let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 288gaogao.scheme // => "http"; 289``` 29023、authority 291``` 292let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 293gaogao.authority // => "gg:gaogao@www.baidu.com:99"; 294``` 29524、ssp 296``` 297let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 298gaogao.ssp " // => gg:gaogao@www.baidu.com:99/path/path?query"; 299``` 30025、userinfo 301``` 302let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 303gaogao.userinfo // => "gg:gaogao"; 304``` 30526、host 306``` 307let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 308gaogao.host // => "www.baidu.com"; 309``` 31027、port 311``` 312let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 313gaogao.port // => "99"; 314``` 31528、query 316``` 317let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 318gaogao.query // => "query"; 319``` 32029、fragment 321``` 322let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 323gaogao.fragment // => "fagment"; 324``` 32530、path 326``` 327let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 328gaogao.path // => "/path/path"; 329``` 33031、equals(Object ob) 331``` 332let gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path1?query#fagment'); 333let gaogao1 = gaogao; 334let res = gaogao.equals(gaogao1); 335console.log(res); // => true; 336``` 33732、normalize() 338``` 339let gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path/66./../././mm/.././path1?query#fagment'); 340let res = gaogao.normalize(); 341console.log(res.path); // => "/path/path1" 342console.log(res.toString()); // => "http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path/path1?query#fagment" 343``` 34433、checkIsAbsolute() 345``` 346let gaogao = new Uri.URI('f/tp://username:password@www.baidu.com:88/path?query#fagment'); 347let res = gaogao.checkIsAbsolute(); 348console.log(res); //=> false; 349``` 35034、toString() 351``` 352let gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path/.././../aa/bb/cc?query#fagment'); 353let res = gaogao.toString(); 354console.log(res.toString()); // => 'http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path/.././../aa/bb/cc?query#fagment'; 355``` 356 357 35835、ConvertXml() 359``` 360var convertml = new convertXml.ConvertXml(); 361``` 36236、convert(xml: string, options: Object) 363``` 364var result = convertml.convert(xml, {compact: false, spaces: 4}); 365``` 36637、new XmlSerializer(buffer: ArrayBuffer | DataView, encoding?: string) 367``` 368 369var arrayBuffer = new ArrayBuffer(1024); 370var bufView = new DataView(arrayBuffer); 371var thatSer = new xml.XmlSerializer(bufView); 372``` 37338、setDeclaration():void 374``` 375var thatSer = new xml.XmlSerializer(bufView); 376thatSer.setDeclaration() // => <?xml version="1.0" encoding="utf-8"?>; 377``` 37839、setCommnet(text: string):void 379``` 380var thatSer = new xml.XmlSerializer(bufView); 381thatSer.setCommnet("Hello, World!"); // => <!--Hello, World!-->; 382``` 38340、setCData(text: string) :void 384``` 385var thatSer = new xml.XmlSerializer(bufView); 386thatSer.setDocType('root SYSTEM "http://www.test.org/test.dtd"'); // => <![CDATA[root SYSTEM \“http://www.test.org/test.dtd\”]]> 387``` 38841、setDocType(text: string):void 389``` 390var thatSer = new xml.XmlSerializer(bufView); 391thatSer.setDocType("foo"); // => <!DOCTYPE foo> 392``` 39342、setNamespace(prefix: string, namespace: string): void 39443、startElement(name: string): void 39544、setAttributes(name: string, value: string): void 39645、endElement(): void 39746、setText(text: string): void 398``` 399var thatSer = new xml.XmlSerializer(bufView); 400thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 401thatSer.startElement("table"); 402thatSer.setAttributes("importance", "high"); 403thatSer.setText("Happy"); 404endElement(); // => <h:table importance="high" xmlns:h="http://www.w3.org/TR/html4/">Happy</h:table> 405``` 40647、addEmptyElement(name: string): void 407``` 408var thatSer = new xml.XmlSerializer(bufView); 409thatSer.addEmptyElement("b"); // => <b/> 410``` 41148、new (buffer: ArrayBuffer | DataView, encoding?: string) 412``` 413var strXml = 414 '<?xml version="1.0" encoding="utf-8"?>' + 415 '<note importance="high" logged="true">' + 416 ' <title>Happy</title>' + 417 '</note>'; 418var arrayBuffer = new ArrayBuffer(strXml.length*2); 419var bufView = new Uint8Array(arrayBuffer); 420var strLen = strXml.length; 421for (var i = 0; i < strLen; ++i) { 422 bufView[i] = strXml.charCodeAt(i);//设置arraybuffer 方式 423} 424var that = new xml.XmlPullParser(arrayBuffer); 425 426``` 42749、parse(option: ParseOptions): void 428``` 429var strXml = 430 '<?xml version="1.0" encoding="utf-8"?>' + 431 '<note importance="high" logged="true">' + 432 ' <title>Happy</title>' + 433 '</note>'; 434var arrayBuffer = new ArrayBuffer(strXml.length*2); 435var bufView = new Uint8Array(arrayBuffer); 436var strLen = strXml.length; 437for (var i = 0; i < strLen; ++i) { 438 bufView[i] = strXml.charCodeAt(i); 439} 440var that = new xml.XmlPullParser(arrayBuffer); 441var arrTag = {}; 442arrTag[0] = '132'; 443var i = 1; 444function func(key, value){ 445 arrTag[i] = 'key:'+key+' value:'+ value.getDepth(); 446 i++; 447 return true; 448} 449var options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}; 450that.parse(options); 451``` 452## Related warehouse 453[js_api_module Subsystem](base/compileruntime/js_api_module-readme.md) 454 455## License 456 457URL is available under [Mozilla license](https://www.mozilla.org/en-US/MPL/), and the documentation is detailed in [documentation](https://gitee.com/openharmony/js_api_module/blob/master/mozilla_docs.txt). See [LICENSE](https://gitee.com/openharmony/js_api_module/blob/master/LICENSE) for the full license text.