The XML content is parsed and returned with a corresponding array object, and the return type "array, JSON" default can be set by the parameter setting: array because it is not good to parse XML with namespaces based on Simplexml_load_string, it will lose namespace content the above is the conclusion that is obtained in the test, and subsequent versions will solve this problem. This can meet the general needs of the use of
- 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 is in conflict with the built-in property, you can customize the name of the relevant property, and the general profile does not need to be set
- $xml _to_array->setattributeasname ("Attributeasname")->setcontentasname ("Contentasname");
- $r = $xml _to_array->parsexml (true);
- Print_r ($R);
- }
- Printing results:
- {"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 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 is in conflict with the built-in property, you can customize the name of the relevant property, and the general profile does not need to be set
- $xml _to_array->setattributeasname ("Attributeasname")->setcontentasname ("Contentasname");
- $r = $xml _to_array->parsexml ();
- Print_r ($R);
- }
- Print results
- 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
- )
- )
- )
- )
- )
- )
Copy Code
- /**
- * Created by JetBrains Phpstorm.
- * User:hedgehog
- * date:12-5-9
- * Time: 4:37
- * 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 properties on a 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 up hierarchical relationships
- 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];
- }
- Additional
- 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;
- }
- }
Copy Code |