2009-07-14 30 views
0

XML建設目前,我在PHP構建一個XML文檔,將產生以下問題用PHP

<root> 
    <collection> 
    <region>1</region> 
    <primary>John</primary> 
    <collection> 
    <collection> 
    <region>1</region> 
    <primary>Jane</primary> 
    <collection> 
    <collection> 
    <region>2</region> 
    <primary>Jill</primary> 
    <collection> 
<root> 

不過,我希望得到如下:

<root> 
    <collection> 
    <region>1</region> 
     <primary>John</primary> 
     <primary>Jane</primary> 
    <collection> 
    <collection> 
    <region>2</region> 
     <primary>Jill</primary> 
    <collection> 
<root> 

要得到第一個XML文檔,我使用以下PHP代碼:

$query = mysql_query("SELECT * FROM eventcal WHERE eventDate = '$date' ORDER BY region"); 

$doc = new DomDocument("1.0"); 

$root = $doc->createElement('data'); 
$root = $doc->appendChild($root); 

if (@mysql_num_rows($query)) { 
    while ([email protected]_fetch_assoc($query)) { 

     $node = $doc->createElement('collection'); 
     $node = $root->appendChild($node); 

     foreach($row as $fieldname => $fieldvalue){ 
      $node->appendChild($doc->createElement($fieldname, $fieldvalue)); 
     } 
    } 
} 

是否有可能將我修改該PHP到有「主」作爲「地區」的孩子嗎?

謝謝!


對不起,夥計們,我的意思是作爲兄弟姐妹的主要。你說得對,收藏標籤會變得多餘。根據您的意見,我認爲結構應改爲:

<root> 
    <collection> 
    <region ID = "1"> 
     <primary>John</primary> 
     <primary>Jane</primary> 
    </region> 
    <region ID = "2"> 
     <primary>Jill</primary> 
    </collection> 
<root> 

我的問題,然後就是,如何能隔離區從是從查詢返回的MySQL的資源父?

謝謝。

+0

第二個示例中的XML與您正在解釋的內容不匹配(或者如何縮進) – Greg 2009-07-14 16:15:04

回答

3

我想你想要的是這樣的:

<?php 

$query = mysql_query("SELECT * FROM eventcal WHERE eventDate = '$date' ORDER BY region"); 

$doc = new DomDocument("1.0"); 

$root = $doc->createElement('data'); 
$root = $doc->appendChild($root); 

$currentRegionId = -1; 
$currentRegionNode = null; 

if (@mysql_num_rows($query)) { 
     while ([email protected]_fetch_assoc($query)) { 

     if ($row['region'] != $currentRegionId) 
     { 
      $currentRegionId = $row['region']; 

      $node = $doc->createElement('collection'); 
      $node = $root->appendChild($node); 

      $currentRegionNode = $doc->createElement('region'); 
      $currentRegionNode->setAttribute('id', $row['region']); 
      $node->appendChild($currentRegionNode); 
     } 

     foreach($row as $fieldname => $fieldvalue){ 
      if ($fieldname != 'region') 
       $currentRegionNode->appendChild($doc->createElement($fieldname, $fieldvalue)); 
     } 
    } 
} 

?> 
+1

Region ID在XML中的位置在哪裏?你會說這是一個好地方嗎? – AnthonyWJones 2009-07-14 16:26:04

+1

目前沒有任何地方 - 我以爲我會在等待問題被糾正的時候插入這個... – Greg 2009-07-14 16:28:01

1

你首先需要了解XML本身更好。你希望區域是一個包含主要元素的複雜類型,因此你可能想引入一個新的元素或屬性來保存區域ID。例如,您可能希望XML看一下這個: -

<root> 
    <collection> 
    <region ID="1"> 
     <primary>John</primary> 
     <primary>Jane</primary> 
    </region> 
    </collection> 
    <collection> 
    <region ID="2"> 
     <primary>Jill</primary> 
    </region> 
    </collection> 
</root> 

注意集合元素是如何成爲當前superflous,因爲它永遠只能擁有一個區域元素。在編寫代碼之前,它的重要性在於您繼續發展您的XML結構實際的