After the XML content is parsed, a corresponding array object is returned, and the return type [array, JSON] can be set through parameter settings. default value: Array Because simplexml_load_string-based parsing of XML with namespaces is not very good, the namespace content will be lost. the above is the conclusion obtained in the test. this problem will be solved in later versions. This can meet general needs.
- Function toJSON ()
- {
- Require_once '../classes/XmlToArray. php ';
- $ XML = <
- "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
- Http://struts.apache.org/dtds/struts-2.0.dtd>
-
-
-
-
-
-
- Add. action
- /Emp/add_suc.jsp
-
-
- /Emp/list. jsp
-
-
- Delete. action
- /Emp/delete_suc.jsp
-
-
- Update. action
- /Emp/edit_suc.jsp
-
-
- /Emp/edit. jsp
-
-
-
- XML;
- Header ("Content-type: text/html; charset = utf-8 ");
- $ Xml_to_array = new XmlToArray ();
- $ Xml_to_array-> setXml ($ XML );
- // When the tag name conflicts with the built-in attribute, you can customize the relevant attribute name.
- // $ Xml_to_array-> setAttributeAsName ("attributeAsName")-> setContentAsName ("contentasName ");
- $ R = $ xml_to_array-> parseXml (true );
- Print_r ($ r );
- }
- // Print the result:
- {"Struts": {"attributes": [], "content": "", "constant": {"attributes": {"name": "struts. objectFactory "," value ":" spring "}," content ":" "}," package ": {" attributes ": {" name ":" crm_employee ", "extends": "struts-default", "namespace": "\/emp"}, "content": "", "action": [{"attributes ": {"name": "add", "class": "addBean", "method": "add"}, "content": "", "result ": [{"attributes": [], "content": "add. action "},{" attributes ": []," content ":" \/emp \/add_suc.jsp "}] },{" attributes ": {" name ": "list", "class": "listBean", "method": "list"}, "content": "", "result": {"attributes": [], "content": "\/emp \/list. jsp "}}, {" attributes ": {" name ":" delete "," class ":" deleteBean "," method ":" delete "}," content ": "", "result": {"attributes": [], "content": "\/emp \/delete_suc.jsp" }}, {"attributes": {"name ": "update", "class": "updateBean", "method": "update"}, "content": "", "result": {"attributes": [], "content": "\/emp \/edit_suc.jsp" }}, {"attributes": {"name": "edit", "class": "editBean ", "method": "edit"}, "content": "", "result": {"attributes": [], "content": "\/emp \/edit. jsp "}}] }}}
- // Print the array
- Function toArray ()
- {
- Require_once '../classes/XmlToArray. php ';
- $ XML = <
- "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
- Http://struts.apache.org/dtds/struts-2.0.dtd>
-
-
-
-
-
-
- Add. action
- /Emp/add_suc.jsp
-
-
- /Emp/list. jsp
-
-
- Delete. action
- /Emp/delete_suc.jsp
-
-
- Update. action
- /Emp/edit_suc.jsp
-
-
- /Emp/edit. jsp
-
-
-
- XML;
- Header ("Content-type: text/html; charset = utf-8 ");
- $ Xml_to_array = new XmlToArray ();
- $ Xml_to_array-> setXml ($ XML );
- // When the tag name conflicts with the built-in attribute, you can customize the relevant attribute name.
- // $ Xml_to_array-> setAttributeAsName ("attributeAsName")-> setContentAsName ("contentasName ");
- $ R = $ xml_to_array-> parseXml ();
- Print_r ($ r );
- }
- // Print the result
- Array
- (
- [Struts] => Array
- (
- [Attributes] => Array
- (
- )
- [Content] =>
- [Constant] => Array
- (
- [Attributes] => Array
- (
- [Name] => struts. objectFactory
- [Value] => spring
- )
- [Content] =>
- )
- [Package] => Array
- (
- [Attributes] => Array
- (
- [Name] => crm_employee
- [Extends] => struts-default
- [Namespace] =>/emp
- )
- [Content] =>
- [Action] => Array
- (
- [0] => Array
- (
- [Attributes] => Array
- (
- [Name] => add
- [Class] => addBean
- [Method] => add
- )
- [Content] =>
- [Result] => Array
- (
- [0] => Array
- (
- [Attributes] => Array
- (
- )
- [Content] => add. action
- )
- [1] => Array
- (
- [Attributes] => Array
- (
- )
- [Content] =>/emp/add_suc.jsp
- )
- )
- )
- [1] => Array
- (
- [Attributes] => Array
- (
- [Name] => list
- [Class] => listBean
- [Method] => list
- )
- [Content] =>
- [Result] => Array
- (
- [Attributes] => Array
- (
- )
- [Content] =>/emp/list. jsp
- )
- )
- [2] => Array
- (
- [Attributes] => Array
- (
- [Name] => delete
- [Class] => deleteBean
- [Method] => delete
- )
- [Content] =>
- [Result] => Array
- (
- [Attributes] => Array
- (
- )
- [Content] =>/emp/delete_suc.jsp
- )
- )
- [3] => Array
- (
- [Attributes] => Array
- (
- [Name] => update
- [Class] => updateBean
- [Method] => update
- )
- [Content] =>
- [Result] => Array
- (
- [Attributes] => Array
- (
- )
- [Content] =>/emp/edit_suc.jsp
- )
- )
- [4] => Array
- (
- [Attributes] => Array
- (
- [Name] => edit
- [Class] => editBean
- [Method] => edit
- )
- [Content] =>
- [Result] => Array
- (
- [Attributes] => Array
- (
- )
- [Content] =>/emp/edit. jsp
- )
- )
- )
- )
- )
- )
- /**
- * Created by JetBrains PhpStorm.
- * User: hedgehog
- * Date: 12-5-9
- * Time: PM
- * To change this template use File | Settings | File Templates.
- */
- Class XmlToArray
- {
- Private $ xml;
- Private $ contentAsName = "content ";
- Private $ attributesAsName = "attributes ";
- Private $ xml_array = array ();
- Public function setXml ($ xmlstr)
- {
- $ This-> xml = $ xmlstr;
- Return $ this;
- }
- Public function setContentAsName ($ name)
- {
- $ This-> contentAsName = $ name;
- Return $ this;
- }
- Public function setAttributeAsName ($ name)
- {
- $ This-> attributesAsName = $ name;
- Return $ this;
- }
- Private function createXMLArray ($ node, & $ parent_node, $ node_index = 0)
- {
- $ Node_attrbutes = array ();
- $ Node_name = $ node-> getName ();
- $ Attributes = $ node-> attributes ();
- $ Children = $ node-> children ();
- // Traverse all attributes on the node
- Foreach ($ attributes as $ attrname => $ attrvalue)
- {
- $ Attrvalue = (string) $ attrvalue;
- $ Node_attrbutes [$ attrname] = trim ($ attrvalue );
- }
- $ Content = "";
- If (count ($ children) = 0)
- {
- $ Content = (string) $ node;
- }
- $ Node_array = array (
- $ This-> attributesAsName => $ node_attrbutes,
- $ This-> contentAsName => trim ($ content)
- );
- // Set the hierarchical relationship
- If (! Isset ($ parent_node [$ node_name])
- {
- $ Is = count ($ parent_node );
- If (! Isset ($ parent_node [$ this-> attributesAsName]) & count ($ parent_node)> 0)
- {
- $ Last_index = count ($ parent_node)-1;
- $ Parent_node = & $ parent_node [$ last_index];
- $ Parent_node [$ node_name] = $ node_array;
- }
- Else
- {
- $ Parent_node [$ node_name] = $ node_array;
- }
- }
- Else
- {
- $ Append = & $ parent_node [$ node_name];
- If (isset ($ append [$ this-> attributesAsName])
- {
- $ Parent_node [$ node_name] = array ($ append );
- $ Append = & $ parent_node [$ node_name];
- }
- If (isset ($ append [$ node_index])
- {
- $ Append = & $ append [$ node_index];
- }
- // Append
- Array_push ($ append, $ node_array );
- }
- $ Index = 0;
- // Recursive operation
- Foreach ($ children as $ childnode)
- {
- $ Parent = & $ parent_node [$ node_name];
- $ This-> createXMLArray ($ childnode, $ parent, $ index ++ );
- }
- Return $ parent_node;
- }
- Public function parseXml ($ isjson = false)
- {
- $ Root = simplexml_load_string ($ this-> xml );
- $ Parent_node = array ();
- $ Array = $ this-> createXMLArray ($ root, $ parent_node );
- Return $ isjson? Json_encode ($ array): $ array;
- }
- }
|