$dom = new DOMDocument;
$MTMessage = $dom->createElement('MTMessage');
$dom->appendChild($MTMessage);
$MTMessage->appendChild($dom->createElement('AppId'));
$MTMessage->appendChild($dom->createElement('AppMsgId'));
$MTMessage->appendChild($dom->createElement('OrgId'));
$MTMessage->appendChild($dom->createElement('MTSourceType'));
$MTMessage->appendChild($dom->createElement('MTSource'));
$MTMessage->appendChild($dom->createElement('Title'));
$MTMessage->appendChild($dom->createElement('Content'));
$MTMessage->appendChild($dom->createElement('Priority'));
$MTMessage->appendChild($dom->createElement('SendTime'));
$MTMessage->appendChild($dom->createElement('ValidTo'));
$MTMessage->appendChild($dom->createElement('Important'));
$MTMessage->appendChild($dom->createElement('AuthCode'));
$MTMessage->appendChild($dom->createElement('NeedReply'));
$MTReceiver = $dom->createElement('MTReceiver');
$MTMessage->appendChild($MTReceiver);
$MTReceiver->appendChild($dom->createElement('ReceiverType'));
$MTReceiver->appendChild($dom->createElement('Receiver'));
$MTReceiver->appendChild($dom->createElement('RecvChannel'));
$MTMessage->getElementsByTagName('AppId')->item(0)->appendChild($dom->createTextNode(127));
$MTMessage->getElementsByTagName('AppMsgId')->item(0)->appendChild($dom->createTextNode(mt_rand()));
$MTMessage->getElementsByTagName('MTSourceType')->item(0)->appendChild($dom->createTextNode(2));
$MTMessage->getElementsByTagName('Content')->item(0)->appendChild($dom->createTextNode($t));
$MTMessage->getElementsByTagName('Important')->item(0)->appendChild($dom->createTextNode(1));
$MTMessage->getElementsByTagName('NeedReply')->item(0)->appendChild($dom->createTextNode('FALSE'));
$MTReceiver->getElementsByTagName('ReceiverType')->item(0)->appendChild($dom->createTextNode(3));
$MTReceiver->getElementsByTagName('Receiver')->item(0)->appendChild($dom->createTextNode($n));
$MTReceiver->getElementsByTagName('RecvChannel')->item(0)->appendChild($dom->createTextNode(1));
$m = $dom->saveHTML();
echo htmlspecialchars($m),"<br />\n";
result:
<MTMessage><AppId>127</AppId><AppMsgId>1294289585</AppMsgId><OrgId></OrgId><MTSourceType>2</MTSourceType><MTSource></MTSource><Title></Title><Content></Content><Priority></Priority><SendTime></SendTime><ValidTo></ValidTo><Important>1</Important><AuthCode></AuthCode><NeedReply>FALSE</NeedReply><MTReceiver><ReceiverType>3</ReceiverType><Receiver></Receiver><RecvChannel>1</RecvChannel></MTReceiver></MTMessage>