Data krijgen uit een xml file
Ik heb een vraag ik ben bezig om een xml file uit te lezen en dit zo te maken dat ik de gewenste output krijg.
Ik zal een voorbeeld geven zodat je weet wat ik bedoel
dit is mijn xml file
Code (php)
1
2
3
4
5
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<config>
<webtitle>PandoraFlyff</webtitle>
<description>Best p-server there is</description>
</config>
<config>
<webtitle>PandoraFlyff</webtitle>
<description>Best p-server there is</description>
</config>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
Class Config {
//Automatic load config.xml file from templates dir
public function __construct() {
$xml = simplexml_load_file("./lib/Template/config.xml");
foreach($xml->children() as $key => $val) {
$this->config[] = $key;
}
print_r($xml);
//return $this->xml;
}
public function Title() {
return $this->config[0];
}
public function Description() {
return $this->config['description'];
}
public function MetaTags() {
}
}
?>
Class Config {
//Automatic load config.xml file from templates dir
public function __construct() {
$xml = simplexml_load_file("./lib/Template/config.xml");
foreach($xml->children() as $key => $val) {
$this->config[] = $key;
}
print_r($xml);
//return $this->xml;
}
public function Title() {
return $this->config[0];
}
public function Description() {
return $this->config['description'];
}
public function MetaTags() {
}
}
?>
Nu wil ik dus de title laten zien
Dit werkt als ik de method
title dus een 0 key mee geef.
Echter zou ik dit graag zo willen
Dit laat alleen niks zien.
Iemand een idee?
Met vriendelijke groet
Toevoeging op 25/03/2014 11:31:04:
Edit: Probleem opgelost door het anders op te roepen.
dit heb ik geprobeerd maar komt niets uit de echo, deze geeft dan de foutmelding 500
Wie kan mij helpen met data uit een xml file halen?
De data bevindt zicht in data/prijzen/article_10000.xlm
<price>
<LV_ARTICLES_PRICE_SHOW>15.300000</LV_ARTICLES_PRICE_SHOW>
</price>
de bedoeling is dat de prijs uit de file wordt gehaald en via een echo word getoond.
Gr. Yoeri
Zet eens var_dump op $xml of $xml->children(), daaruit kan je meestal wel afleiden hoe je uit moet lezen.
$xml = simplexml_load_file("data/prijzen/article_10000.xlm"); om de file te openen
dan om het uit te lezen
echo $xml->LV_ARTICLES_PRICE_SHOW;
Dat is alles.
Toevoeging op 25/03/2014 20:25:04:
Hmm ik heb nu het volgende probleem
Ik wil dat ik een value kan aanpassen in de xml file
dus stel we hebben dit
<config>
<web_slider>1</web_slider>
<allow>TRUE</allow>
</config
nu probeer ik het zo te veranderen
Code (php)
1
2
3
4
5
6
2
3
4
5
6
<?php
$xml = simplexml_load_string("<web_slider>$string</web_slider>");
$xml->web_slider[0] = '0';
echo $xml->asXML("./lib/Template/config.xml");
?>
$xml = simplexml_load_string("<web_slider>$string</web_slider>");
$xml->web_slider[0] = '0';
echo $xml->asXML("./lib/Template/config.xml");
?>
Output is nu dit
<web_slider><web_slider>0</web_slider></web_slider>
Hoe kan ik dan een value veranderen zonder dat alles verandert wordt.
Heb al veel gelezen op diverse fora maar kom er niet zo 1,2,3 uit.
Deze zijn d.m.v. een id code gekoppeld aan de site.
Echter hoe maak ik in php duidelijk dat hij de data uit de goede file haald?
hebben nu dit:
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<?php
class Article extends CustomXML implements ArrayAccess,IteratorAggregate,Countable{
private $directory,$file,$rowtag,$prefix;
public $data=array();
function __construct($id,$rowtag = "Article",$prefix = "LV_ARTICLES_") {
$this->rowtag = $rowtag; $this->prefix = $prefix;
if (is_numeric($id)) { $this->file = $this->directory."article_$id.xml"; } else { $this->file = $id; }
?>
class Article extends CustomXML implements ArrayAccess,IteratorAggregate,Countable{
private $directory,$file,$rowtag,$prefix;
public $data=array();
function __construct($id,$rowtag = "Article",$prefix = "LV_ARTICLES_") {
$this->rowtag = $rowtag; $this->prefix = $prefix;
if (is_numeric($id)) { $this->file = $this->directory."article_$id.xml"; } else { $this->file = $id; }
?>
Gewijzigd op 27/03/2014 17:12:01 door Yoeri Achterbergen