SimpleXML is a PHP extension that allows us to easily manipulate and get XML data.


PHP SimpleXML – Get Node Values

Get the node values from the “note.xml” file:

Example

The output of the code above will be:

Tove
Jani
Reminder
Don’t forget me this weekend!

Another XML File

Assume we have an XML file called “books.xml“, that looks like this:



  
    Everyday Italian
    Giada De Laurentiis
    2005
    30.00
  
  
    Harry Potter
    J K. Rowling
    2005
    29.99
  
  
    XQuery Kick Start
    James McGovern
    2003
    49.99
  
  
    Learning XML
    Erik T. Ray
    2003
    39.95
  

PHP SimpleXML – Get Node Values of Specific Elements

The following example gets the node value of the <title> element in the first and second <book> elements in the “books.xml” file:

Example

The output of the code above will be:

Everyday Italian
Harry Potter

PHP SimpleXML – Get Node Values – Loop

The following example loops through all the <book> elements in the “books.xml” file, and gets the node values of the <title>, <author>, <year>, and <price> elements:

Example

The output of the code above will be:

Everyday Italian, Giada De Laurentiis, 2005, 30.00
Harry Potter, J K. Rowling, 2005, 29.99
XQuery Kick Start, James McGovern, 2003, 49.99
Learning XML, Erik T. Ray, 2003, 39.95

PHP SimpleXML – Get Attribute Values

The following example gets the attribute value of the “category” attribute of the first <book> element and the attribute value of the “lang” attribute of the <title> element in the second <book> element:

Example

The output of the code above will be:

COOKING
en

PHP SimpleXML – Get Attribute Values – Loop

The following example gets the attribute values of the <title> elements in the “books.xml” file:

Example

The output of the code above will be:

en
en
en-us
en-us

More PHP SimpleXML

For more information about the PHP SimpleXML functions, visit our PHP SimpleXML Reference.

[css] body {
color: #000000;
}[/css]

Affect of Color Property on Borders and Oulines

The color property is not just for text content, but for anything in the foreground that takes a color value. For instance, if border-color or outline-color value hasn’t been defined explicitly for the element, the color value will be used instead.

[css]

p.one {
color: #0000ff;
border: 2px solid;
}
p.two {
color: #00ff00;
outline: 2px solid;
}

[/css]