Asked  7 Months ago    Answers:  5   Viewed   34 times

Hello this is my first post. i'm trying to parse an xml using php, but i'm stuck with it

example of my xml :

<xml xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
<s:Schema id='RowsetSchema'>
   <s:ElementType name='row' content='eltOnly' rs:CommandTimeout='30'>
      <s:AttributeType name='ows_LinkTitle' rs:name='Phone Number*' rs:number='1'>
         <s:datatype dt:type='string' dt:maxLength='512' />
      <s:AttributeType name='ows_GSM_x0020_PUK' rs:name='GSM PUK' rs:number='2'>
         <s:datatype dt:type='string' dt:maxLength='512' />
   <z:row ows_LinkTitle='628558621183' ows_GSM_x0020_PUK='41016732'/>
   <z:row ows_LinkTitle='628558621181' ows_GSM_x0020_PUK='21783670'/>

i'm trying to get the value from each of 'z:row' Any help is very appreciated. Thanks

i'm trying this code and it doesnt work

$xml_all = simplexml_load_string($xmlnya);

echo '<ol>';
foreach ($xml_all->xml->rs->z as $zrow)
 echo '<li> ows_LinkTitle: '.$zrow['ows_LinkTitle'];
 echo '</li>';
echo '</ol>';



The snippets below assume the (fixed) XML document is a string in $xml.


$xml  = simplexml_load_string($xml);
$rows = $xml->children('rs', TRUE)->data->children('z', TRUE)->row;
foreach ($rows as $row) {
    $attributes = $row->attributes();
    $link_title = (string) $attributes->ows_LinkTitle;
    $gsm_puk    = (string) $attributes->ows_GSM_x0020_PUK;
    var_dump($link_title, $gsm_puk);

To read: SimpleXML usage, SimpleXMLElement::children(), SimpleXMLElement::attributes().


$doc = new DOMDocument;
$rows = $doc->getElementsByTagNameNS('#RowsetSchema', 'row');
foreach ($rows as $row) {
    $link_title = $row->getAttribute('ows_LinkTitle');
    $gsm_puk    = $row->getAttribute('ows_GSM_x0020_PUK');
    var_dump($link_title, $gsm_puk);

To read: DOMDocument::getElementsByTagNameNS(), DOMElement::getAttribute().

Saturday, May 29, 2021
answered 7 Months ago

Figured out the issue was with the XML feed rather than code:

XML feed was missing this line:

<r25:events xmlns:r25="" xmlns:xl="" pubdate="2010-05-19T13:58:08-04:00">

Thanks for the help though.

Saturday, May 29, 2021
answered 7 Months ago

You can use an xpath query to target the node names that starts with Foto, then after getting those nodes, then just use foreach:

$xml_string = '<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi=""  xsi:noNamespaceSchemaLocation="propiedades.xsd" generated="2015-04-28T15:16:46">
<Descripcion>Excelente casa de dos pisos en condominio en la urbanización Santa Maria De Villa Club, en Carabayllo, con un AT:92m2 y un AC:113m2, cuenta con 3 iluminadas habitaciones, 3 baños, cochera, de estreno, con áreas verdes, piscina, cancha de fútbol, salones para actividades. ¡Para más información contactarse con nuestros agente!ID:   35751</Descripcion>

$xml = simplexml_load_string($xml_string);
$fotos = $xml->xpath('//*[substring(name(), 1, 4) = "Foto"]');
foreach($fotos as $foto) {
    echo $foto, '<br/>';

Sample Output

If you don't want to use the xpath route, just use ->getName to check the node name and use normal PHP string functions to check it:

$xml = simplexml_load_string($xml_string);
foreach($xml->propiedades->children() as $element) {
    if(substr($element->getName(), 0, 4) === 'Foto') {
        echo $element, '<br/>';

Sample Output

Saturday, May 29, 2021
answered 7 Months ago

This was how i have eventually done it using XMLReader:


define ('XMLFILE', '');
echo "<pre>";

$items = array ();
$i = 0;

$xmlReader = new XMLReader();
$xmlReader->open(XMLFILE, null, LIBXML_NOBLANKS);

$isParserActive = false;
$simpleNodeTypes = array ("title", "description", "media:title", "link", "author", "pubDate", "guid");

while ($xmlReader->read ())
    $nodeType = $xmlReader->nodeType;

    // Only deal with Beginning/Ending Tags
    if ($nodeType != XMLReader::ELEMENT && $nodeType != XMLReader::END_ELEMENT) { continue; }
    else if ($xmlReader->name == "item") {
        if (($nodeType == XMLReader::END_ELEMENT) && $isParserActive) { $i++; }
        $isParserActive = ($nodeType != XMLReader::END_ELEMENT);

    if (!$isParserActive || $nodeType == XMLReader::END_ELEMENT) { continue; }

    $name = $xmlReader->name;

    if (in_array ($name, $simpleNodeTypes)) {
        // Skip to the text node
        $xmlReader->read ();
        $items[$i][$name] = $xmlReader->value;
    } else if ($name == "media:thumbnail") {
        $items[$i]['media:thumbnail'] = array (
                "url" => $xmlReader->getAttribute("url"),
                "width" => $xmlReader->getAttribute("width"),
                "height" => $xmlReader->getAttribute("height"),
                "type" => $xmlReader->getAttribute("type")
    } else if ($name == "media:content") {
        $items[$i]['media:content'] = array (
                "url" => $xmlReader->getAttribute("url"),
                "width" => $xmlReader->getAttribute("width"),
                "height" => $xmlReader->getAttribute("height"),
                "filesize" => $xmlReader->getAttribute("fileSize"),
                "expression" => $xmlReader->getAttribute("expression")

echo "</pre>";

Saturday, July 31, 2021
answered 4 Months ago

Currently your XML is invalid since the test namespace is not declared, you can declare it like this:

<item xmlns:test="">

Having this you can use XNamespace to qualify the XML element you want with the correct namespace:

XElement xdoc = XElement.Parse(e.Result);
XNamespace test = "";
this.newsList.ItemsSource = from item in xdoc.Descendants("item")
                            select new ArticlesItem
                                LinkID = item.Element(test + "link_id").Value,
                                Title = item.Element("title").Value,
                                Description = this.Strip(item.Element("description").Value).Substring(0, 200).ToString()
Wednesday, August 25, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :