This repository was archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathXmlUtil.php
More file actions
91 lines (86 loc) · 2.42 KB
/
Copy pathXmlUtil.php
File metadata and controls
91 lines (86 loc) · 2.42 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
<?php
/**
* Copyright 2013 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/**
* Utilities for manipulating SimpleXMLElement objects.
*
* @package Splunk
* @internal
*/
class Splunk_XmlUtil
{
/**
* Returns whether the specified XML element exists.
*
* @param SimpleXMLElement $xml
* @return boolean
*/
public static function elementExists($xml)
{
return $xml->getName() != '';
}
/**
* @param SimpleXMLElement $xml
* @param string $attributeName
* @return string|NULL
*/
public static function getAttributeValue($xml, $attributeName)
{
return (isset($xml->attributes()->$attributeName))
? (string) $xml->attributes()->$attributeName
: NULL;
}
/**
* @param SimpleXMLElement $xml
* @return string
*/
public static function getTextContent($xml)
{
// HACK: Some versions of PHP 5 can't access the [0] element
// of a SimpleXMLElement object properly.
return (string) $xml;
}
/**
* @param SimpleXMLElement $xml
* @param string $xpathExpr
* @return string|NULL
*/
public static function getTextContentAtXpath($xml, $xpathExpr)
{
$matchingElements = $xml->xpath($xpathExpr);
return (count($matchingElements) == 0)
? NULL
: Splunk_XmlUtil::getTextContent($matchingElements[0]);
}
/**
* Returns true if the specified SimpleXMLElement represents a unique
* element or false if it represents a collection of elements.
*
* @param SimpleXMLElement $xml
* @return bool
*/
public static function isSingleElement($xml)
{
$count = 0;
foreach ($xml as $item)
{
$count++;
if ($count >= 2)
return false;
}
return ($count == 1);
}
}