2012-05-09 2 views
1

가능한 중복 :
PHP convert XML to JSONPHP를 통해 XML 페이지를 JSON으로 자동 변환하는 방법이 있습니까?

다음

이처럼 내 출력 XML의 내 샘플이 모습입니다 :

<item> 
<Barcode>0602527522593</Barcode> 
<albumid>1818</albumid> 
<Title>Gold</Title> 
<Country>Sweden</Country> 
<Format>CD</Format> 
<Length></Length> 
<Number_of_Discs></Number_of_Discs> 
<Type>album</Type> 
<Description></Description> 
<Band_or_Artist>Adams, Ryan</Band_or_Artist> 
</item> 

가 사용하기 쉬운 거기에 내장 된 PHP 함수 또는 추가 기능을 사용하여 JSON으로 신속하게 변환하고 출력 할 수 있습니까? 내장되지 않은 경우 어떤 확장 프로그램을 사용해야합니까?

+2

, 그냥 원본 데이터가 아니라 XML을 생성하고 JSON로 변환보다, 무엇이든에서 JSON을 생성하는 것이 더 쉬울 수 있습니다. 데이터의 출처는 어디입니까? – Jazz

답변

1

SimpleXML (http://php.net/manual/en/book.simplexml.php)을 사용하여 XML 입력을 읽은 다음 json_encode (http://php.net/)를 읽을 수 있습니다. manual/en/book.json.php)을 사용하여 출력을 만듭니다.

아마도 노드 목록을 반복하면서 태그 이름을 키로 사용하여 모든 것을 배열에 넣고 싶다면 매우 간단해야합니다.

0

이와 비슷한? 이 XML이 출력라고 때문에

<?php 

$xmlstr = "<item> 
<Barcode>0602527522593</Barcode> 
<albumid>1818</albumid> 
<Title>Gold</Title> 
<Country>Sweden</Country> 
<Format>CD</Format> 
<Length></Length> 
<Number_of_Discs></Number_of_Discs> 
<Type>album</Type> 
<Description></Description> 
<Band_or_Artist>Adams, Ryan</Band_or_Artist> 
</item>"; 

$movies = new SimpleXMLElement($xmlstr); 
echo $output = json_encode($movies); 
?> 
관련 문제