/    Sign up×
Community /Pin to ProfileBookmark

xml rendering in php

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

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@tarsusOct 12.2006 — 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:

[code=php]$xml=simplexml_load_file('file.xml');
echo $xml->myelement;[/code]


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.
Copy linkTweet thisAlerts:
@bokehOct 12.2006 — [URL=http://www.php.net/dom]The DOM functions![/URL]
Copy linkTweet thisAlerts:
@nihaauthorOct 13.2006 — 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
Copy linkTweet thisAlerts:
@bokehOct 13.2006 — any suggestions! thank you all in advance.[/QUOTE]You really need to read the manual. I already provide a generic link above. To load an XML string you need [URL=http://www.php.net/manual/en/function.dom-domdocument-loadxml.php]DOMDocument->loadXML()[/URL]. Also since PHP 5, [I]new[/I] returns by reference automatically so using =& (as in your code above) in this context is deprecated and produces E_STRICT level message.
Copy linkTweet thisAlerts:
@nihaauthorOct 13.2006 — 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.
Copy linkTweet thisAlerts:
@bokehOct 13.2006 — You are not loading it into a file, you are loading it into an object. This allows you to use methods like [URL=http://www.php.net/manual/en/function.dom-domdocument-getelementsbytagname.php]DOMDocument->getElementsByTagName()[/URL] and [URL=http://www.php.net/manual/en/function.dom-domdocument-getelementbyid.php]DOMDocument->getElementById()[/URL] 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.
Copy linkTweet thisAlerts:
@nihaauthorOct 13.2006 — ok thank you very muchi will try this

Niha
Copy linkTweet thisAlerts:
@nihaauthorOct 16.2006 — 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 $[B]noanswer-forward[/B]) //gives an error here

{

echo $line -> getAttribute('public-alias').'<br>';

}

any suggestion how to overcome the dash!

Thanks

Noha
Copy linkTweet thisAlerts:
@bokehOct 16.2006 — [I]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.[/I]
Copy linkTweet thisAlerts:
@nihaauthorOct 16.2006 — 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');

}
Copy linkTweet thisAlerts:
@bokehOct 16.2006 — [CODE]$params = $dom->getElementsByTagName('[B][color=red]noanswer-forward[/color][/B]');

foreach ($params as $[B][color=red]noanswer-forward[/color][/B]) //gives an error here
{
echo $line -> getAttribute('public-alias').'<br>';
}[/CODE]
[/QUOTE]
There is no connection between the naming of these two. Just use a legal variable name in the loop.
Copy linkTweet thisAlerts:
@nihaauthorOct 16.2006 — 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
Copy linkTweet thisAlerts:
@bokehOct 16.2006 — The should not be mark-up elements inside cdata but if there are these will be considered as cdata and not document nodes.
×

Success!

Help @niha spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...