2016-11-01 6 views
0

D'abord, je suis un débutant total quand il s'agit de PHP. Je travaille sur un site web pour un refuge pour animaux (je suis un vétérinaire/codeur) et nous obtenons des données d'une base de données en ligne d'animaux disponibles pour adoption. Ce fichier XML a une sortie comme celui-ci (ce qui est juste une petite partie du fichier xml):xml à json en utilisant php, l'échec d'inclure xml <string>

<TotalWeight>64.5 lbs </TotalWeight> 
<UnitWeight>lbs</UnitWeight> 
<AdditionalPhotoUrls> 
<string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55806.jpg</string> 
    <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55807.jpg</string> 
    <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55809.jpg</string> 
    <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55810.jpg</string> 
    <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55876.jpg</string> 
    <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55877.jpg</string> 
    <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_71558.jpg</string> 
    <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_71559.jpg</string> 
</AdditionalPhotoUrls> 
<AdoptionAmount>0.0000</AdoptionAmount> 

Le fichier php a ce code qui est converti en JSON:

$item['totalweight'] = (string)$animal->TotalWeight; 
$item['weight'] = (string)$animal->UnitWeight; 
$item['photosX'] = (string)$animal->AdditionalPhotoUrls; 

la sortie JSON à un fichier, en utilisant

echo file_put_contents('page.json', json_encode($data)); 

est réussie pour et UnitWeight mais poids total pas AdditionalPhotoUrls, avec ses cordes. La sortie de fils est comme celui-ci (encore une fois, une petite partie):

{ 
"weight": "lbs", 
"totalweight": "65", 
"photosX": "\n  \n  \n  \n  \n  \n  \n  \n  \n " 
}, 

Je suis pas clair sur la façon dont je forme: (string)$animal->AdditionalPhotoUrls; pour ne pas obtenir les chaînes finissent plutôt alors avec \ n

à obtenir tous les multiples <string>. Actuellement, le fichier json est vide pour photosX.

Toute aide ou direction serait appréciée.

Répondre

0

ce xml ne contient pas d'attributs, de sorte que nous pouvons facilement saisir:

<?php $xml = '<root> <TotalWeight>64.5 lbs </TotalWeight> <UnitWeight>lbs</UnitWeight> <AdditionalPhotoUrls> <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55806.jpg</string> <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55807.jpg</string> <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55809.jpg</string> <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55810.jpg</string> <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55876.jpg</string> <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_55877.jpg</string> <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_71558.jpg</string> <string>https://eastbayspcapets.shelterbuddy.com//photos/lostfound/doc_71559.jpg</string> </AdditionalPhotoUrls> <AdoptionAmount>0.0000</AdoptionAmount> </root>'; $json = json_encode(simplexml_load_string($xml)); echo $json;

+0

mis à jour ma question. – Macsupport