Questions tagged [simplexml]

A PHP extension shipped with the main source tree. The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators. For the Java "Simple" XML framework, please use the [simple-framework] tag.

The SimpleXML extension provides a simple toolset to convert XML to an object that can be processed with normal property selectors and array iterators.

Once an XML document is converted, the object obtained will reference the root element of the document. The object's properties will reflect the child nodes and uses the same name as the nodes. This principle works recursively across the document.

3932 questions
58
votes
6 answers

Parse XML with Namespace using SimpleXML

I have this as xml: Learn QB in Minutes
user38968
  • 661
  • 1
  • 8
  • 8
57
votes
5 answers

How to write CDATA using SimpleXmlElement?

I have this code to create and update xml file: '); $xml->title = 'Site Title'; $xml->title->addAttribute('lang', 'en'); $xml->saveXML($xmlFile); ?> This generates the…
quantme
  • 3,609
  • 4
  • 34
  • 49
57
votes
18 answers

Remove a child with a specific attribute, in SimpleXML for PHP

I have several identical elements with different attributes that I'm accessing with SimpleXML: I need to remove a specific seg element,…
TimTowdi
  • 647
  • 1
  • 6
  • 7
51
votes
3 answers

Format output of $SimpleXML->asXML();

The title pretty much says it all. If I have something like (from PHP site examples): $xmlstr = << XML; $sxe = new SimpleXMLElement($xmlstr); $sxe->addAttribute('type',…
Eli
  • 97,462
  • 20
  • 76
  • 81
50
votes
2 answers

How can I check if a object is an instance of a specific class?

Is there a way to check if an object is an SimpleXMLELement? private function output_roles($role) { foreach ($role as $current_role) { $role_ = $current_role->attributes(); $role_type = (string) $role_->role; echo…
Snickbrack
  • 1,253
  • 4
  • 21
  • 56
42
votes
1 answer

Using XML node names with hyphens in PHP

I am trying to extract some data from XML but when I execute the following I get a Warning: Invalid argument supplied for foreach() in ... Code Example: foreach ($xml->custom-field-value as $milestone) { ... } It works fine for node…
williamsdb
  • 1,054
  • 2
  • 17
  • 29
41
votes
4 answers

Getting actual value from PHP SimpleXML node

$value = $simpleXmlDoc->SomeNode->InnerNode; actually assigns a simplexml object to $value instead of the actual value of InnerNode. If I do: $value = $simpleXmlDoc->SomeNode->InnerNode . "\n"; I get the value. Anyway of getting the actual value…
James
  • 6,471
  • 11
  • 59
  • 86
39
votes
6 answers

XML parser error: entity not defined

I have searched stackoverflow on this problem and did find a few topics, but I feel like there isn't really a solid answer for me on this. I have a form that users submit and the field's value is stored in a XML file. The XML is set to be encoded…
NightHawk
  • 3,633
  • 8
  • 37
  • 56
39
votes
4 answers

PHP library for parsing XML with a colons in tag names?

I've been trying to use SimpleXML, but it doesn't seem to like XML that looks like this: sample italic text So what library will handle tags that look like that (have a colon in them)?
mpen
  • 272,448
  • 266
  • 850
  • 1,236
38
votes
2 answers

How to save changed SimpleXML object back to file?

So, I have this code that searches for a particular node in my XML file, unsets an existing node and inserts a brand new child node with the correct data. Is there a way of getting this new data to save within the actual XML file with simpleXML? …
ThinkingInBits
  • 10,792
  • 8
  • 57
  • 82
36
votes
2 answers

SimpleXML: Selecting Elements Which Have A Certain Attribute Value

In an XML document, I have elements which share the same name, but the value of an attribute defines what type of data it is, and I want to select all of those elements which have a certain value from the document. Do I need to use XPath (and if…
dimo414
  • 47,227
  • 18
  • 148
  • 244
36
votes
3 answers

PHP - SimpleXML - AddChild with another SimpleXMLElement

I'm trying to build a rather complex XML document. I have a bunch of sections of the XML document that repeats. I thought I'd use multiple string templates as base document for the sections and create instances of XML elements using…
Ben
  • 20,737
  • 12
  • 71
  • 115
32
votes
5 answers

SimpleXMLElement to PHP Array

Variable $d comes from file_get_contents function to a URL. $answer = @new SimpleXMLElement($d); Below is output of the print_r($answer): SimpleXMLElement Object ( [Amount] => 2698 [Status] => OK [State] => FL [Country] => USA ) How can I…
Codex73
  • 5,690
  • 11
  • 56
  • 76
29
votes
1 answer

SimpleXml how to correctly set encoding and xmins?

Any ideas on how I can get PHPs SimplXMLElement to kick off with the following? The main root will then be: Or do I use…
Cups
  • 6,901
  • 3
  • 26
  • 30
28
votes
7 answers

Recursive cast from SimpleXMLObject to Array

I need to recursively cast a PHP SimpleXMLObject to an array. The problem is that each sub element is also a PHP SimpleXMLElement. Is this possible?
Glen Solsberry
  • 11,960
  • 15
  • 69
  • 94