2014-02-24 3 views
1

xml에 내 mysql 결과를 인쇄하고 싶습니다. 이것은 지금까지 해봤습니다 :PHP mysql 쿼리 결과 xml

포함 ('dbconnect.php');

$sql = "SELECT verse_id, verse_title, verse_content, lang FROM verses WHERE lang = 'English'"; 
$stmt = $conn->prepare($sql); 
$stmt->execute(); 

$set = array(); 

while($r = $stmt->fetchAll(PDO::FETCH_ASSOC)){ 
    $set = $r; 
} 

$xml = new SimpleXMLElement('<root/>'); 
array_walk_recursive($set, array($xml, 'addChild')); 
print $xml->asXML(); 


?> 

그러나 다음과 같이 표시하는 것 :

<verse> 
<verse_id>1</verse_id> 
<verse_title>John 3:16</verse_title> 
<verse_content>For God so loved the world...</verse_content> 
<lang>English</lang> 
</verse> 

내가 잘못 알고하지 않습니다하지만 당신은이 작업을 수행하는 방법을 알고있는 경우 :

<1>verse_id<27>verse_title<"Peace I leave with you; my peace I give you. I do not give to you as the world gives. Do not let your hearts be troubled and do not be afraid. >verse_contentlang<2> 

I는 다음과 같이 표시 할 도와 주시면 고맙겠습니다.

답변

1

대신 SimpleXMLElementDOMDocument를 사용하고 formatOutputtrue의 값을 설정할 수 있습니다

$doc = new DOMDocument('1.0', 'UTF-8'); 
$doc->formatOutput = true; 

<?php 

// "Create" the document. 
$xml = new DOMDocument("1.0", "UTF-8"); 
$xml->formatOutput = true; 

// Create some elements. 
$xml_album = $xml->createElement("Album"); 
$xml_track = $xml->createElement("Track", "The ninth symphony"); 

// Set the attributes. 
$xml_track->setAttribute("length", "0:01:15"); 
$xml_track->setAttribute("bitrate", "64kb/s"); 
$xml_track->setAttribute("channels", "2"); 

// Create another element, just to show you can add any (realistic to computer) number of sublevels. 
$xml_note = $xml->createElement("Note", "The last symphony composed by Ludwig van Beethoven."); 

// Append the whole bunch. 
$xml_track->appendChild($xml_note); 
$xml_album->appendChild($xml_track); 

// Repeat the above with some different values.. 
$xml_track = $xml->createElement("Track", "Highway Blues"); 

$xml_track->setAttribute("length", "0:01:33"); 
$xml_track->setAttribute("bitrate", "64kb/s"); 
$xml_track->setAttribute("channels", "2"); 
$xml_album->appendChild($xml_track); 

$xml->appendChild($xml_album); 
// Parse the XML. 
print '<pre>' . htmlentities($xml->saveXML()) . '</pre>'; 
?> 

출력

+0

완전한 답변을 제공해 주시겠습니까? 죄송합니다 이걸 이해할 수 없다 – Dunkey

+0

좋은 예가 http://ir1.php.net/domdocument#82447에 있습니다 ... –

+0

예를 따라했는데 요소가없는 레코드를 표시하는 것일뿐입니다 – Dunkey