xmlparsetest.php
5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
class xml2array{
public static function parse($xml){
return self::parsetag(simplexml_load_string($xml));
}
private static function parsetag($xml,$ns=NULL,$rootXML=NULL){
if(!$rootXML)$rootXML=$xml;
$tagName=$xml->getName();
if($ns)$tagName=$ns.':'.$tagName;
$array=array();
$array[$tagName]['@attributes']=self::getAttributes($xml,$rootXML);
if(self::hasChildren($xml,$rootXML)){
$children=self::getChildren($xml,$rootXML);
echo '<b>'.$tagName.'</b><br /><pre>'.print_r($children,true).'</pre>';
foreach($children as $fullChildName=>$childCollection){
//$child=$childCollection;
$childName=split(':',$fullChildName);
foreach($childCollection as $child){
$childParsed=self::parsetag($child,$childName[0],$rootXML);
//$cIndex=count($array[$tagName][$childName]);
$array[$tagName][]=$childParsed;
}
}
}else{
$array[$tagName]['value']=(string)$xml;
}
return $array;
}
private static function hasChildren($xml,$rootXML){
return count(self::getChildren($xml,$rootXML))>0;
}
private static function getAttributes($xml,$rootXML){
$attr=array();
$namespaces=$rootXML->getNamespaces(true);
foreach($namespaces as $namespace=>$uri){
$nsAttrs=(array)$xml->attributes($uri);
$nsAttrs=isset($nsAttrs['@attributes'])?$nsAttrs['@attributes']:array();
foreach($nsAttrs as $nsAttr=>$nsAttrVal){ //TODO: Support for multiple same name tags
$attr[$namespace.':'.$nsAttr]=$nsAttrVal;
}
}
return $attr;
}
private static function getChildren($xml,$rootXML){
$children=array();
$namespaces=$rootXML->getNamespaces(true);
foreach($namespaces as $namespace=>$uri){
$nsChildren=$xml->children($uri);
foreach($nsChildren as $nsChild){ //TODO: Support for multiple same name tags
$childRealName=$namespace.':'.$nsChild->getName();
if(!isset($children[$childRealName]))$children[$childRealName]=array();
if(!is_array($children[$childRealName]))$children[$childRealName]=array();
$children[$childRealName][]=$nsChild;
}
}
return $children;
}
}
$xml='<?xml version="1.0" encoding="utf-8"?>
<service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:cmis="http://www.cmis.org/2008/05" xmlns:alf="http://www.alfresco.org">
<workspace cmis:repositoryRelationship="self">
<atom:title>Main Repository</atom:title>
<cmis:repositoryInfo>
<cmis:repositoryId>0f91f397-7cd1-479b-9f56-266affe188d8</cmis:repositoryId>
<cmis:repositoryName>Main Repository</cmis:repositoryName>
<cmis:repositoryRelationship>self</cmis:repositoryRelationship>
<cmis:repositoryDescription></cmis:repositoryDescription>
<cmis:vendorName>Alfresco</cmis:vendorName>
<cmis:productName>Alfresco Repository (Labs)</cmis:productName>
<cmis:productVersion>3.0.0 (Stable 1526)</cmis:productVersion>
<cmis:rootFolderId>http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore/Company%20Home</cmis:rootFolderId>
<cmis:capabilities>
<cmis:capabilityMultifiling>true</cmis:capabilityMultifiling>
<cmis:capabilityUnfiling>false</cmis:capabilityUnfiling>
<cmis:capabilityVersionSpecificFiling>false</cmis:capabilityVersionSpecificFiling>
<cmis:capabilityPWCUpdateable cmis:bla="bleh">true</cmis:capabilityPWCUpdateable>
<cmis:capabilityPWCSearchable>true</cmis:capabilityPWCSearchable>
<cmis:capabilityAllVersionsSearchable>false</cmis:capabilityAllVersionsSearchable>
<cmis:capabilityQuery>both</cmis:capabilityQuery>
<cmis:capabilityJoin>nojoin</cmis:capabilityJoin>
<cmis:capabilityFullText>fulltextandstructured</cmis:capabilityFullText>
</cmis:capabilities>
<cmis:cmisVersionsSupported>0.5</cmis:cmisVersionsSupported>
<cmis:repositorySpecificInformation></cmis:repositorySpecificInformation>
</cmis:repositoryInfo>
<collection href="http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore/Company%20Home/children" cmis:collectionType="root-children">
<atom:title>root collection</atom:title>
</collection>
<collection href="http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore/Company%20Home/descendants" cmis:collectionType="root-descendants">
<atom:title>root collection</atom:title>
</collection>
<collection href="http://localhost:8080/alfresco/service/api/checkedout" cmis:collectionType="checkedout">
<atom:title>checkedout collection</atom:title>
</collection>
<collection href="http://localhost:8080/alfresco/service/api/unfiled" cmis:collectionType="unfiled">
<atom:title>unfiled collection</atom:title>
</collection>
<collection href="http://localhost:8080/alfresco/service/api/types" cmis:collectionType="types-children">
<atom:title>type collection</atom:title>
</collection>
<collection href="http://localhost:8080/alfresco/service/api/types" cmis:collectionType="types-descendants">
<atom:title>type collection</atom:title>
</collection>
<collection href="http://localhost:8080/alfresco/service/api/query" cmis:collectionType="query">
<atom:title>query collection</atom:title>
</collection>
</workspace>
</service>
';
$sxml=simplexml_load_string($xml);
$struct=json_decode(json_encode($sxml),true);
echo '<pre>'.htmlentities($xml).'</pre>';
//echo '<hr /><pre>'.print_r($struct,true).'</pre>';
//echo '<hr /><pre>'.print_r($sxml,true).'</pre>';
//cho '<hr /><pre>'.print_r(xml2array($xml),true).'</pre>';
echo '<hr /><pre>'.print_r(xml2array::parse($xml),true).'</pre>';
//echo http_get_request_headers();
?>