/    Sign up×
Community /Pin to ProfileBookmark

XML Documents

When I load XML documents through ajax and try to output them, I always get either an error or the xml code only showing, so for a while now I’ve been using a php file that turns the xml document into a html document and then outputs it and my ajax then just prints out the results, but due to the fact im making a portable js library now, I want to know how I can correctly output a xml document, while using css to style the outputted data.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManJun 04.2009 — what's the entire code of your PHP page that generates the XML document?
Copy linkTweet thisAlerts:
@vitronauthorJun 04.2009 — Hey Aaron,

Here's the function I use to read the xml data into an array:-
[code=php]function getXmlData($xml_data) {
$n=0;
$ar=array();
$parser = xml_parser_create();
xml_parse_into_struct($parser,$xml_data,$vals,$index) or die(xml_error_string(xml_get_error_code($parser)));
xml_parser_free($parser);
$ttags=array();
for($n=0;$n<=count($vals)-1;$n++) {
if(trim($vals[$n][value])) {
$ar[$vals[$n][tag]][count($ar[$vals[$n][tag]])]=$vals[$n][value];
$ttags[$vals[$n][tag]]=$vals[$n][tag];
}
}
$tags=array();
foreach($ttags as $tagi) { array_push($tags,$tagi); }
return $ar;
}[/code]

Note, that the above code I found on the internet, dont remember where though because that was a long time ago.

To output this in html, I use this (assuming im getting my blog posts):-
[code=php]$myar=getXmlData($blogposts_data);
for($i=0;$i<=count($myar[TITLE]);$i++) {
$title=$myar[TITLE][$i];
$text=$myar[DESCRIPTION][$i];
$link=$myar[LINK][$i];
$category=$myar[CATEGORY][$i];
$author=$myar[AUTHOR][$i];
$pubDate=$myar[PUBDATE][$i];
if(isset($_GET['cat'])){
if($_GET['cat'] != $category){
continue;
}
}
if($title) {
$pagedata .= <<<EOF
<div class="blog_posts">
<h3>$title</h3>
$text<br />
<font id="pubdate">$pubDate | By $author in $category</font><br /><br />
</div>
EOF;
}

}[/code]

Thats it, as long as I use this, even if I get a xml file through this using ajax, it displays correctly, but I dont want that, what I want to do is load the xml file directly in javascript, and then parse it, creating the required elements, and attach it to the target element. I'll worry about styling later, just want to do this right now ?
×

Success!

Help @vitron 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.28,
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,
)...