php XML create <! - ?adf version =“1.0”? - > tag

时间:2012-03-06 01:23:19

标签: php xml domdocument

我正在尝试将adf xml文件发布到crm。

到目前为止,我已经能够创建这个

<?xml version="1.0" encoding="UTF-8"?>

<adf version="1.0">

<prospect>

但需要创建此

<?xml version="1.0" encoding="UTF-8" ?>

  <?adf version="1.0"?>

      <adf>

      <prospect>

我似乎找不到创建标记的方法。需要帮助。

我正在尝试使用php dom提出adf xml文件,该文件应该如下所示

我试图在php中使用dom来提出adf xml文档,例如

  <?ADF VERSION="1.0"?> 
    <adf> 
      <prospect> 
        <id sequence="uniqueLeadId" source="name of promotion"></id> 
        <requestdate>2008-06-25T8:47:50</requestdate> 
        <vehicle interest="buy" status="used"> 
          <vin>2G4WF52L6S1472882</vin> 
          <year>1995</year> 
          <make>BUICK</make> 
          <model>Regal</model> 
          <stock>12886</stock> 
        </vehicle> 
        <customer> 
          <contact> 
          <name part="first" type="individual">Johnny</name> 
          <name part="last" type="individual">Customer</name> 
          <email>johnny@customer.com</email> 
          <phone type="home">857-485-7485</phone> 
          <phone type="work">867-586-7584</phone> 
          <phone type="mobile">979-685-9685</phone> 
        </contact> 
        <comments>What is your best price?</comments> 
      </customer> 
      <vendor> 
        <contact> 
            <name part="full">Name of lead provider or web-site</name> 
            <email>johnny@vendor.com</email> 
            <phone type="business">857-485-7485</phone> 
        </contact> 
      </vendor> 
    </prospect> 
</adf>

我能够创建文件唯一的问题是在xml上创建adf声明。我只能创建一个元素并为其分配一个属性,例如

$root = $xmlDoc->appendChild($xmlDoc->createElement("adf"));

$root->setAttribute("version","1.0");

3 个答案:

答案 0 :(得分:4)

$pi = $xmlDoc->createProcessingInstruction('adf', 'version="1.0"');

答案 1 :(得分:0)

创建“处理指令”API,而不是创建元素:

http://www.php.net/manual/en/domdocument.createprocessinginstruction.php

答案 2 :(得分:0)

创建和保存示例ADF xml文件的示例...

//create the xml document
$xmlDoc = new DOMDocument();
$adfImplementation = new DOMImplementation();
$doc = $adfImplementation->createDocument(NULL, "ADF");
$doc->xmlVersion = "1.0";

//creating an xslt adding processing line
$xslt = $xmlDoc->createProcessingInstruction('adf', 'version="1.0"');

//adding it to the xml
$xmlDoc->appendChild($xslt);

//create the root element
$root = $xmlDoc->appendChild($xmlDoc->createElement("adf"));

$prospect = $root->appendChild($xmlDoc->createElement("prospect"));
$prospect->appendChild($xmlDoc->createElement("requestdate"))->appendChild($xmlDoc->createTextNode(date(DATE_ATOM)));
$xmlDoc->save('newadf.xml');

结果文件将为:

<?xml version="1.0"?>

<?adf version="1.0"?>

<adf>

   <prospect>

       <requestdate>2019-06-03T11:09:36+02:00</requestdate>

   </prospect>

</adf>