我有一个运行良好的PHP代码,但是运行不正常。 F.Y.I::它正在运行一个从表单收集提交的数据的功能。 (此处未包含代码段,因为可以)。 我需要在“ Dados”中包含2个字段:名称和电子邮件,但是只记录了 email 。
我该怎么办?有任何线索吗?
我的实际PHP代码:
function my_generate_xml($posted_data)
$domDocument = new DOMDocument('1.0', 'ISO-8859-1');
$domDocument->formatOutput = true;
// build maximizer xml file
$xml_root = $domDocument->createElement('moduledata');
$xmlEntity = $domDocument->createElement('entity');
$xmlEntityTN = $domDocument->createAttribute('tablename');
$xmlEntityTN->value = 'Ent';
$xmlEntityFN = $domDocument->createAttribute('formatname');
$xmlEntityFN->value = 'Curriculum';
$xmlEntity->appendChild($xmlEntityTN);
$xmlEntity->appendChild($xmlEntityFN);
$xmlDefine = $domDocument->createElement('define');
$xmlDefine->nodeValue = $posted_data['nome'];
$xmlDefineN = $domDocument->createAttribute('name');
$xmlDefineN->value = 'ParamNome';
$xmlDefine->appendChild($xmlDefineN);
$xmlEntity->appendChild($xmlDefine);
$xmlSiga = $domDocument->createElement('SigaFiles');
$xmlSigaDN = $domDocument->createAttribute('Text');
$xmlSigaDN->value = 'SQG';
$xmlSiga->appendChild($xmlSigaDN);
// create node for current dados
$xml_dados = $domDocument->createElement('Dados');
$domElement = $domDocument->createElement('attribute',$posted_data['nome']);
$domElement = $domDocument->createElement('attribute',$posted_data['email']);
$domAttribute = $domDocument->createAttribute('domainname');
// Value for the created attribute
$domAttribute->value = 'Nome';
$domAttribute->value = 'Email';
$domElement->appendChild($domAttribute);
$xml_dados->appendChild($domElement);
$xmlSiga->appendChild($xml_dados);
$xmlEntity->appendChild($xmlSiga);
$xml_root->appendChild($xmlEntity);
$domDocument->appendChild($xml_root);
所需的XML输出格式:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<moduledata>
<entity tablename="Ent" formatname="Curriculum">
<define name="ParamNome">Thomas Edison</define>
<SigaFiles Text="SQG">
<Dados>
<attribute domainname="Nome">Thomas Edison</attribute>
<attribute domainname="Email">thomas.edison@gmail.com</attribute>
</Dados>
</SigaFiles>
</entity>
</moduledata>
实际的XML输出格式:
<?xml version="1.0" encoding="ISO-8859-1"?>
<moduledata>
<entity tablename="Ent" formatname="Curriculum">
<define name="ParamNome">Thomas Edison</define>
<SigaFiles Text="SQG">
<Dados>
<attribute domainname="Email">thomas.edison@gmail.com</attribute>
</Dados>
</SigaFiles>
</entity>
</moduledata>
实际XML输出格式:(已更新)
<?xml version="1.0" encoding="ISO-8859-1"?>
<moduledata>
<entity tablename="Ent" formatname="Curriculum">
<define name="Thomas Edison">nome</define>
<SigaFiles Text="SQG">
<Dados>
<attribute domainname="Nome">Thomas Edison</attribute>
<attribute domainname="Email">thomas.edison@gmail.com</attribute>
</Dados>
</SigaFiles>
</entity>
</moduledata>
PHP代码(已更新)
<?php
function my_generate_xml($posted_data)
{
$domDocument = new DOMDocument('1.0', 'ISO-8859-1');
$domDocument->formatOutput = true;
// build maximizer xml file
$xml_root = $domDocument->createElement('moduledata');
$xmlEntity = $domDocument->createElement('entity');
$xmlEntityTN = $domDocument->createAttribute('tablename');
$xmlEntityTN->value = 'Ent';
$xmlEntityFN = $domDocument->createAttribute('formatname');
$xmlEntityFN->value = 'Curriculum';
$xmlEntity->appendChild($xmlEntityTN);
$xmlEntity->appendChild($xmlEntityFN);
$xmlDefine = $domDocument->createElement('define');
$xmlDefine->nodeValue = $posted_data['nome'];
$xmlDefineN = $domDocument->createAttribute('name');
$xmlDefineN->value = 'ParamNome';
$xmlDefine->appendChild($xmlDefineN);
$xmlEntity->appendChild($xmlDefine);
$xmlSiga = $domDocument->createElement('SigaFiles');
$xmlSigaDN = $domDocument->createAttribute('Text');
$xmlSigaDN->value = 'SQG';
$xmlSiga->appendChild($xmlSigaDN);
// create node for current dados
$xml_dados = $xmlSiga->appendChild($domDocument->createElement('Dados'));
$domElement = $xml_dados->appendChild($domDocument->createElement('attribute'));
$domElement->appendChild($domDocument->createTextNode($posted_data['nome']));
$domElement->setAttribute('domainname', 'Nome');
$domElement = $xml_dados->appendChild($domDocument->createElement('attribute'));
$domElement->appendChild($domDocument->createTextNode($posted_data['email']));
$domElement->setAttribute('domainname', 'Email');
$domDocument->appendChild($domElement);
$xmlSiga->appendChild($domDocument);
$xmlEntity->appendChild($xmlSiga);
$xml_root->appendChild($xmlEntity);
$domDocument->appendChild($xml_root);
// save it as a file for further processing
$content = chunk_split(base64_encode($domDocument->saveXML()));
$uploads = wp_upload_dir();
$domDocument->save($uploads['basedir'].'/prorh/'.(int)microtime(true).'.xml');
}
?>
答案 0 :(得分:1)
请考虑在创建例程后重新组织例程并追加元素,不要在最后保存所有appendChild
调用。由于重复使用相同的变量名,因此将所有组件(元素,属性,值)保持在一起。回忆XML是从根开始生长的树结构:
// build maximizer xml file
$domDocument = new DOMDocument('1.0', 'ISO-8859-1');
$domDocument->formatOutput = true;
// moduledata root element
$xml_root = $domDocument->createElement('moduledata');
$domDocument->appendChild($xml_root);
// entity element
$xmlEntity = $domDocument->createElement('entity');
$xml_root->appendChild($xmlEntity);
$xmlEntityTN = $domDocument->createAttribute('tablename');
$xmlEntityTN->value = 'Ent';
$xmlEntityFN = $domDocument->createAttribute('formatname');
$xmlEntityFN->value = 'Curriculum';
$xmlEntity->appendChild($xmlEntityTN);
$xmlEntity->appendChild($xmlEntityFN);
// define element
$xmlDefine = $domDocument->createElement('define');
$xmlEntity->appendChild($xmlDefine);
$xmlDefine->nodeValue = $posted_data['nome'];
$xmlDefineN = $domDocument->createAttribute('name');
$xmlDefineN->value = 'ParamNome';
$xmlDefine->appendChild($xmlDefineN);
// SigaFiles element
$xmlSiga = $domDocument->createElement('SigaFiles');
$xmlEntity->appendChild($xmlSiga);
$xmlSigaDN = $domDocument->createAttribute('Text');
$xmlSigaDN->value = 'SQG';
$xmlSiga->appendChild($xmlSigaDN);
// dados element
$xml_dados = $domDocument->createElement('Dados');
$xmlSiga->appendChild($xml_dados);
// attribute child nodes
$domElement = $domDocument->createElement('attribute', $posted_data['nome']);
$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Nome';
$domElement->appendChild($domAttribute);
$xml_dados->appendChild($domElement);
$domElement = $domDocument->createElement('attribute', $posted_data['email']);
$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Email';
$domElement->appendChild($domAttribute);
$xml_dados->appendChild($domElement);
// OUTPUT TREE TO STRING
header("Content-type: text/xml");
echo $domDocument->saveXML();
答案 1 :(得分:0)
您覆盖$domElement
变量而不附加节点。
// create node for current dados
$xml_dados = $domDocument->createElement('Dados');
$domElement = $domDocument->createElement('attribute',$posted_data['nome']);
$domElement = $domDocument->createElement('attribute',$posted_data['email']);
$domAttribute = $domDocument->createAttribute('domainname');
我建议将create*
个调用嵌套在appendChild()
个调用中。
// create node for current dados
$xml_dados = $xmlSiga->appendChild($domDocument->createElement('Dados'));
$domElement = $xml_dados->appendChild($domDocument->createElement('attribute'));
$domElement->appendChild($domDocument->createTextNode($posted_data['nome']));
$domElement->setAttribute('domainname', 'Nome');
$domElement = $xml_dados->appendChild($domDocument->createElement('attribute'));
$domElement->appendChild($domDocument->createTextNode($posted_data['email']));
$domElement->setAttribute('domainname', 'Email');
您无需将属性创建为节点,DOMElement::setAttribute()
可以正常工作。但是您应该创建文本内容作为节点。 DOMDocument :: createElement()的第二个参数已损坏,并且仅转义了一些特殊字符。