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 回答