Hej.
Jako ze z problemem samego xmla sobie poradzilem teraz przyszla pora na parser.
Najgorsze jest to, ze pliki ktore parsuje maja zroznicowana strukture i niektore dzialaja a niektore nie.
Moze umieszcze to, co do tej pory udalo mi sie zdzialac.
<?php
class XML {
public function __construct() {
}
public function setHTTP($httpAddress) {
$this->httpAddress = $httpAddress;
}
private function getHTTP() {
return $this->httpAddress;
}
public function authorisation($user, $password) {
$this->user = $user;
$this->password = $password;
}
private function getUser() {
return $this->user;
}
private function getPassword() {
return $this->password;
}
public function addParameter($key, $value) {
$this->parameters[$key] = $value;
}
private function getParameters() {
return $this->parameters;
}
private function parametersPart() {
$first = true;
$outString = "";
$parameters = $this->getParameters();
if(!empty($parameters)) { foreach($parameters as $key=>$value) {
if($first) {
$outString .= "?".$key."=".$value;
$first = false;
} else {
$outString .= "&".$key."=".$value;
}
}
}
return $outString;
}
private function getFullUrl() {
$parameters = $this->parametersPart();
if(!empty($parameters)) { return $this->getHTTP().$this->parametersPart();
} else {
return $this->getHTTP();
}
}
public function getXml() {
$xml = curl_init();
curl_setopt($xml, CURLOPT_URL, $this->getFullUrl());
curl_setopt($xml, CURLOPT_RETURNTRANSFER, true);
curl_setopt($xml, CURLOPT_USERPWD, $this->getUser().":".$this->getPassword());
curl_setopt($xml, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($xml);
curl_close($xml);
return simplexml_load_string($output);
}
public function objectToArray($object) {
$arrayOb = get_object_vars($object);
return $arrayOb;
}
public function finalArray() {
$mainArray = $this->objectToArray($this->getXml());
foreach($mainArray as $key=>$value) {
foreach($this->objectToArray($value) as $k=>$v) {
foreach($this->objectToArray($v) as $ka=>$va) {
//echo "---- K: ".$ka." , V: ".$va."<BR>";
}
foreach($v as $ka=>$va) {
//echo "---- K: ".$key." , V: ".$value."<BR>";
}
} else {
//echo "-- K: ".$key." , V: ".$value."<BR>";
}
}
foreach($value as $k=>$v) {
foreach($this->objectToArray($v) as $ka=>$va) {
//echo "---- K: ".$ka." , V: ".$va."<BR>";
}
foreach($v as $ka=>$va) {
//echo "---- K: ".$key." , V: ".$value."<BR>";
}
} else {
//echo "-- K: ".$key." , V: ".$value."<BR>";
}
}
} else {
$values[$key] = $value;
//echo "K: ".$key." , V: ".$value."<BR>";
}
}
return $values;
}
public function getValues() {
return $this->finalArray();
}
public $httpAddress;
public $user;
public $password;
public $parameters = array();
}
?>
(echo jest typowo pogladowo oczywiscie (IMG:
style_emoticons/default/tongue.gif) )
i teraz np jeden zrzut otrzymuje w miare dobrze:
XML
<award-categories type="array">
<award-category>
<id type="integer">340</id>
<name>Po meczu</name>
</award-category>
<award-category>
<id type="integer">341</id>
<name>Na wyjeździe</name>
</award-category>
<award-category>
<id type="integer">342</id>
<name>Trofea smakoszy</name>
</award-category><award-category>
<id type="integer">343</id>
<name>Dla zawodowców</name>
</award-category>
</award-categories>
wynik na ekranie
-- K
: @attributes
, V
: Array ---- K: id , V: 340
---- K: name , V: Po meczu
---- K: id , V: 341
---- K: name , V: Na wyjeździe
---- K: id , V: 342
---- K: name , V: Trofea smakoszy
---- K: id , V: 343
---- K: name , V: Dla zawodowców
a inny na tym samym schemacie sie krzaczy:
XML
<product-award>
<available-id type="NilClass">2</available-id>
<award-type-id type="NilClass">2</award-type-id>
<can-be-minus-id type="NilClass">1</can-be-minus-id>
<catalog-code type="NilClass">B15</catalog-code>
<catalog-name type="NilClass">Zestaw bitewny Clone Trooper LEGO</catalog-name>
<id type="integer">12352</id>
<min-quantity-for-order type="NilClass">1</min-quantity-for-order>
<point-value-for-catalog type="NilClass">770</point-value-for-catalog>
</product-award>
wynik na ekranie
-- K
: @attributes
, V
: Array ---- K
: @attributes
, V
: Array -- K: head , V:
-- K: head , V:
---- K
: @attributes
, V
: Array ---- K: h1 , V: We're sorry, but something went wrong.
---- K: p , V: We've been notified about this issue and we'll take a look at it shortly.
Wychodzi na to, ze powinienem wejsc jeszcze glebiej w strukture... ale moznaby to bylo w takim razie mnozyc i mnozyc...
zastanawialem sie nad jakas funkcja ktora bedzie zapetlac warunki az do ostatniego poziomu obiektu.. ale jeszcze nie wymyslilem
Bede wdzieczny za jakies przemyslenia, opinie, propozycje i wskazowki.
Nie chcialbym skonczyc temu na tekscie "bo w internecie jest tego pelno".. moze i jest, ale forum tez jest po to, zeby uzyskiwac informacje.
Pozdro
Wasp
Ten post edytował Wasper 17.10.2012, 15:18:58