0

I'm trying to read xml file in zend FW2 but xml reader doesn't read attributes and values together from the tags so I'm trying to use simplexml to read my xml file(this is the first time I write xml file) xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<xs:mapping xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:IM-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <xs:serverSide xsi:type="IM-ENC:Array"></xs:serverSide>
    <xs:clientSide xsi:type="IM-ENC:Array">
        <item xsi:type="ns2:Map">
            <item>
                <key xsi:type="xs:string">key1</key>
                <value xsi:type="xs:string">value1</value>
            </item>
            <item>
                <key xsi:type="xs:string">key2</key>
                <value xsi:type="xs:string">value2</value>
            </item>
        </item>
    </xs:clientSide>
</xs:mapping>

and reading the file this way:

<?php
$xml=simplexml_load_file(my.xml);
print_r($xml);

but the result is empty simpleXMLElement object, I can't figure out the mistake in my xml can you help me, thanx in advance. NOTE: I'm using this file for local use not soap, but couldn't find another example to use array, maybe this changes a thing.

rramiii
  • 1,156
  • 6
  • 15
  • 29
  • Surround `my.xml` in single quotes. Like this.. `$xml=simplexml_load_file('my.xml');` – Shankar Narayana Damodaran Apr 13 '14 at 14:03
  • Thank you for your respond, I tried this but still have the empty object – rramiii Apr 13 '14 at 14:08
  • Please enable errors as suggested in my answer and update your question with the messages that will appear. – ek9 Apr 13 '14 at 14:12
  • Thank you .. tried your answer and the response still "object(SimpleXMLElement)#225 (0) { }" :( – rramiii Apr 13 '14 at 14:17
  • Even while you look at the output of `print_r` from a `SimpleXMLelment` and it's *looking* empty, the **SimpleXMLElement** is not empty at all. It's just that `print_r` or `var_dump` is just not a good fit for *SimpleXMLelement*. Instead use the `asXML()` method of **SimpleXMLElement** to just output the XML. You then see it's not empty at all. – hakre Aug 03 '14 at 19:21

2 Answers2

2

Please enable error reporting and paste the messages that you get. Also, encapsulate filename in single-quotes as Shankar suggested:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$xml=simplexml_load_file('my.xml'); // note the quotes
print_r($xml);

The error messages should give you enough information to properly debug this further. If you need further assistance then please update your question with error messages.

ek9
  • 3,392
  • 5
  • 23
  • 34
  • Thank you for fast respond, I'v tried it and all I get is: "object(SimpleXMLElement)#225 (0) { }" – rramiii Apr 13 '14 at 14:14
  • This is exptected result and loads fine. You are loading xml scheme file. What are you trying to accomplish? – ek9 Apr 13 '14 at 14:17
  • the whole idea is to map key tag with value tag, that is why I have to load it and read it by xml reader. – rramiii Apr 13 '14 at 14:23
0

found it, the error in xml file as I thought, it should be like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:mapping xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:IM-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <serverSide xsi:type="IM-ENC:Array"></serverSide>
    <clientSide xsi:type="IM-ENC:Array">
        <item xsi:type="ns2:Map">
            <item>
                <key xsi:type="xs:string">key1</key>
                <value xsi:type="xs:string">value1</value>
            </item>
            <item>
                <key xsi:type="xs:string">key2</key>
                <value xsi:type="xs:string">value2</value>
            </item>
        </item>
    </clientSide>
</xs:mapping>

as you see I just removed the name space from tag name (xs name space). in fact I can't find an explanation why, but it worked for me. if any one has an explanation why please share it with us. thanks all for helping Me.

rramiii
  • 1,156
  • 6
  • 15
  • 29