2
$domtree = new DOMDocument('1.0', 'UTF-8');

/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("xml");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
//create the  root Envelope
$currentTrack = $domtree->createElement("Envelope");

$currentTrack = $xmlRoot->appendChild($currentTrack);
/* you should enclose the following two lines in a cicle */
//create the first child node
$c=  $currentTrack->appendChild($domtree->createElement('Header'));
$c->appendChild($domtree->createElement('AccountId','04af6cbcfb9f3038281b06389803d577'));

ここでは、ヘッダーに「version = 2.1」を追加しています。追加するにはどうすればよいですか?

4

1 に答える 1

0

このsetAttribute()を次のように試してください。

    $domtree = new DOMDocument('1.0', 'UTF-8');

    /* create the root element of the xml tree */
    $xmlRoot = $domtree->createElement("xml");
    /* append it to the document created */
    $xmlRoot = $domtree->appendChild($xmlRoot);
    //create the  root Envelope
    $currentTrack = $domtree->createElement("Envelope");

    $currentTrack = $xmlRoot->appendChild($currentTrack);
    /* you should enclose the following two lines in a cicle */
    //create the first child node
    $header = $domtree->createElement('Header');
    $header->setAttribute('version', '2.1');
    $c = $currentTrack->appendChild($header);
    $c->appendChild($domtree->createElement('AccountId', '04af6cbcfb9f3038281b06389803d577'));

    $domtree->save('x.xml');
于 2012-04-10T12:39:54.013 に答える