Skip to content
Puritys Chen edited this page May 19, 2015 · 6 revisions

If you have a XML file, it's name is the "test.xml" and the content is like the following,

 <root>
   <user>
    <age>13</age>
   </user>
 </root>

How to parse a XML Content.

var php = require('phplike/module');
var dom = new php.DOMDocument();
var json = dom.load('test.xml');
// or you could use:  json = dom.json; 
// It will get the json format too.

In the process of parsing the XML content, phplike will convert the xml to be a json format. This format will be like the following:

{
  "name": "root",
  "value": "",
  "childNodes": [
    {
       "name": "user",
       "value": "",
       "childNodes": [
         {
           "name": "age",
           "value": 13
         }
       ]
    }
  ]
}

Phplike also support method getElementsByTagName, you could find any elements by the tag name, and the return value will be a array inlcude many DOMElement, it is like the php object http://php.net/manual/en/class.domelement.php.

For example:

var php = require('phplike/module');
var dom = new php.DOMDocument();
dom.load('test.xml');
var userList = dom.getElementsByTagName("user");
var user = userList[0];
var value = user.nodeValue;

Clone this wiki locally