1

私が抱えていた問題は、XML に書き込むたびにルート XML が生成されていたことです。主な問題は、子の設定とルートの定義でした。Łza の助けにより、ルート XML ノードが無視されることがわかりました。次に、子をセットアップして作成し、コンテンツを追加します。正しい形式の例は次のとおりです。

$xml = simplexml_load_file('FILENAME.xml');  // Load XML File Need to add IF Statment to create if does not exist

$result = $xml->addchild('Result'); // Ignore Root NODE and Add Child Results

$result->addChild('Time', gmdate('D-M-Y -H:i:s')); // Rest of the below adds Child to Result and outputs results
$result->addChild('Channel', $Site);
$result->addChild('Type', '**');
$result->addChild('Process', $Status);
$result->addChild('SKU', $code->SKU);
$result->addChild('item', $item);
$result->addChild('Status', '$Feedback');
$result->addChild('ErrorID', '$Error');
$result->addChild('Message', '$Message');

$xml->asXml('FILENAME.xml');  //Write to file would be

// All of the above Code is using variables from another part of the script

出力は次のようになります

<Root>
    <Result>
        <Time>Fri-May-2013 -09:15:22</Time>
        <Channel>20</Channel>
        <Type>**</Type>
        <Process>Update</Process>
        <SKU>98746524765</SKU>
        <Item/>
        <Status>Problem</Status>
        <ErrorID>999-Error</ErrorID>
        <Message>Unknown file format support</Message>
    </Result>
    <Result>
        <Time>Fri-May-2013 -09:15:22</Time>
        <Channel>20</Channel>
        <Type>**</Type>
        <Process>Update</Process>
        <SKU>5412254785</SKU>
        <Item/>
        <Status>Problem</Status>
        <ErrorID>123-Error</ErrorID>
        <Message>Invalid Item</Message>
    </Result>
</Root>

ありがとう

4

1 に答える 1

0

ハードコーディングされた xml 作成の代わりに、SimpleXMLElement ライブラリを使用してみてください。これは、最初は使用するのがより複雑かもしれませんが、はるかに安全 (xml をハードコードするときに xml 構造で発生する可能性のあるエラーを回避することを意味します) であり、使い始めたばかりのときは簡単に使用できます。ノード、子ノードの追加/削除も簡単です。

これはあなたのコードの例です:

$xml = new SimpleXMLElement('<xml/>');

$data = $xml->addChild('data');
$result = $data->addChild('Result');
$result->addChild('Time', gmdate('D-M-Y -H:i:s'));
$result->addChild('Channel', $SiteID);

// ... and the same way create all your xml nodes.
// if you want add next <result> node witch all elements repeat the code, (or put it in loop if you want more <result> elements):
$result = $data->addChild('Result');
$result->addChild('Time', gmdate('D-M-Y -H:i:s'));
$result->addChild('Channel', $SiteID);

// and after create all nodes save the file:
$xml->asXml('DHError.xml'); 

上記のコードは xml を作成します:

 <xml>
  <data>
   <Result>
     <Time>Fri-May-2013 -12:14:39</Time> 
     <Channel>data</Channel> 
   </Result>
   <Result>
     <Time>Fri-May-2013 -12:14:39</Time> 
     <Channel>data</Channel> 
   </Result>
  </data>
 </xml>

それでおしまい。次に、xml を読み込んで処理する必要がある場合は簡単です。

ファイルをロードするには、次を使用します。

$xml2 = simplexml_load_file('DHError.xml');

// to add new node <Result>:
$resultNext = $xml2->data->addchild('Result');
$resultNext->addChild('Time', gmdate('D-M-Y -H:i:s'));
$resultNext->addChild('Channel', $SiteID);

//and save file
$xml2->asXml('DHError.xml');

これはxmlを作成します:

<?xml version="1.0" ?> 
 <xml>
  <data>
   <Result>
     <Time>Fri-May-2013 -12:27:24</Time> 
     <Channel>data</Channel> 
   </Result>
   <Result>
     <Time>Fri-May-2013 -12:27:24</Time> 
     <Channel>data</Channel> 
  </Result>
  <Result>
    <Time>Fri-May-2013 -12:27:24</Time> 
    <Channel>data</Channel> 
  </Result>
 </data>
</xml>
于 2013-05-10T12:19:45.520 に答える