x2js是一個 實現(xiàn) XML 與 JavaScript 對象之間相互轉(zhuǎn)換的工具庫。轉(zhuǎn)換并不一定是無損的,但十分便利。
XML 轉(zhuǎn)換為 JavaScript 對象
- var x2js = new X2JS(); var document = x2js.xml2js("1"); console.log(document.foo); // 輸出:Object {a: "1"}
JavaScript 對象轉(zhuǎn)換為 XML
- var xml = x2js.js2xml({foo: {a: 1}}); console.log(xml); // 輸出:1
- var x2js2 = new X2JS(); var xml = `Code BlogNic RaboyNicRaboyMariaCampos`;
- var document = x2js2.xml2js(xml);
- console.log(document.business.company); // 輸出:Code Blog
- console.log(document.business.owner); // 輸出:Nic Raboy
- console.log(document.business.employee[0].firstname); // 輸出:Nic
- console.log(document.business.employee[0].lastname); // 輸出:Raboy
新聞熱點
疑難解答