Click to See Complete Forum and Search --> : xml rendering in php


niha
10-12-2006, 09:36 AM
hi everyone,
I would like to know how can i render an xml string(for example $data) that i alerady have inside my php code by manipulating the attributes values within the string. as(echo $valueofattributeinxml)

any ideas are very appreciated.
sorry for the hassel but i am a newbe in php and xml

thanks in advance
Niha

tarsus
10-12-2006, 02:47 PM
In order to work with XML data with PHP, you need a special PHP library installed and enabled - libxml2. What version of PHP are you running? The library is enabled by default in PHP 5. Run phpinfo() on a separate page to check your settings and see if there is a section entitled libxml.

Unfortunately, I can't tell you where to get the library if you don't have it, nor am I familiar with using the library. But its own functions work something like:

$xml=simplexml_load_file('file.xml');
echo $xml->myelement;

Whether it has any functions to create XML data from something other than an XML file and then interpret that, I don't know, but it seems likely.

bokeh
10-12-2006, 02:55 PM
The DOM functions! (http://www.php.net/dom)

niha
10-13-2006, 02:57 AM
hi guys, thank you for your replies but actually my xml is not in a file, actually it is in a string...and i am using php5 so the dom and xmllib is already installed. here is my code
i am sending a request to the xml database through post headers and i am getting back the reply in a string.

$req =& new HTTP_Request($url);
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addHeader("Content-Type", "text/xml");
$req->addRawPostData($data, true);
$req->sendRequest();

//here is the xml so i need to manipulate this string to get a certain value.
$str=$req->getResponseBody();

any suggestions! thank you all in advance.
Niha

bokeh
10-13-2006, 03:40 AM
any suggestions! thank you all in advance.You really need to read the manual. I already provide a generic link above. To load an XML string you need DOMDocument->loadXML() (http://www.php.net/manual/en/function.dom-domdocument-loadxml.php). Also since PHP 5, new returns by reference automatically so using =& (as in your code above) in this context is deprecated and produces E_STRICT level message.

niha
10-13-2006, 03:49 AM
ok but loading it , means i m loading the string into a document an xml file .... actually my target is to get the string (in real time) and then render the php page in the browser like html layout...rendering as well a certian value that i can extract from the string. already knowing the type of xml response i am getting for sure.

bokeh
10-13-2006, 04:05 AM
You are not loading it into a file, you are loading it into an object. This allows you to use methods like DOMDocument->getElementsByTagName() (http://www.php.net/manual/en/function.dom-domdocument-getelementsbytagname.php) and DOMDocument->getElementById() (http://www.php.net/manual/en/function.dom-domdocument-getelementbyid.php) to access the various nodes of the document. Once you have extracted the nodes you require it is simply a matter of creating the HTML output.

niha
10-13-2006, 05:54 AM
ok thank you very muchi will try this
Niha

niha
10-16-2006, 03:25 AM
after finally getting and listing the variables and nodes i am having one more problem
- some xml tags contain dash so when I call it it gives a php error Parse error: parse error, unexpected '-',

$params = $dom->getElementsByTagName('noanswer-forward');

foreach ($params as $noanswer-forward) //gives an error here
{
echo $line -> getAttribute('public-alias').'<br>';
}

any suggestion how to overcome the dash!
Thanks
Noha

bokeh
10-16-2006, 05:01 AM
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

niha
10-16-2006, 05:18 AM
hi bokeh

the variable i am talking about is refering to an xml tag inside a cdata... the first problem i dont know why but i cant get any tag within the cdata , second i am having a probelm due to the tag names having minus within I am trying to access the ><noanswer-forward > tag

this is the xml tag

<call-handling id="12102" code="0" name="My default rules" line-link="12097" template="basic:basic" status="on">
- <![CDATA[
<user-script><incoming-call status="on"><anonymous-filter status="off" baring-type="voicemail"/>
<reject status="off/><noanswer-forward status="on" current="voicemail"/></incoming-call></user-script>
]]>
</call-handling>

***php code***

$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($str);
$params = $dom->getElementsByTagName('reject');

foreach ($params as $reject) {
echo $reject -> getAttribute('status');
}

bokeh
10-16-2006, 05:29 AM
$params = $dom->getElementsByTagName('noanswer-forward');

foreach ($params as $noanswer-forward) //gives an error here
{
echo $line -> getAttribute('public-alias').'<br>';
}There is no connection between the naming of these two. Just use a legal variable name in the loop.

niha
10-16-2006, 05:36 AM
how if there should be a connection a relation , anyway putting anythng else i dont have results. and when i choose another tag as reject which is included in the cdata it id not returning anythng!!!
any idea how to access the tags inside the cdata

bokeh
10-16-2006, 06:54 AM
The should not be mark-up elements inside cdata but if there are these will be considered as cdata and not document nodes.