Php parse simple xml attributes

I am having problems with the xml file, I found many examples / tutorials on the Internet and on this site, but I do not see one like this:

I have an XML file, for example:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<items item1="1" item2="2" item3="3" item4="4"/>
<items item1="1" item2="2" item3="3" item4="4"/>
<items item1="1" item2="2" item3="3" item4="4"/>
</root>

I have not seen the data is the same as before, and when I try:

<?php

$obj = simplexml_load_string("sample.xml");
print_r($obj);
?>

I get an error message:

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found

I launched the xml file through a check at www.xmlvalidation.com and everything returns, am I missing something?

I am looking to be able to echo element attribute values ​​in a while loop.

+3
source share
3 answers

You are trying to load XML from a file using simplexml_load_string()one that expects a line input. Use instead simplexml_load_file().

$obj = simplexml_load_file("sample.xml");
+8
source

, , , </root>, <root>

0

. XML , , .

0

All Articles