Php 使用DOM文档和SimpleXML格式化xml。啊

Php 使用DOM文档和SimpleXML格式化xml。啊,php,xml,simplexml,domdocument,Php,Xml,Simplexml,Domdocument,可能重复: 是否可以同时使用SimpleXML和DOM文档 我使用SimpleXML获取一个文件,更新它并保存它,但是xml的格式是在一条长线上 如何在脚本中使用DOM文档格式化输出?是否必须在SimpleXML位hs完成写入文件后执行,或者可以在写入之前执行 谢谢//发布wordpress $postID=获取_ID(); $postData=get_post($posted); //echo$posted.''.$postData->post_title.'.$postData->post

可能重复:

是否可以同时使用SimpleXML和DOM文档

我使用SimpleXML获取一个文件,更新它并保存它,但是xml的格式是在一条长线上

如何在脚本中使用DOM文档格式化输出?是否必须在SimpleXML位hs完成写入文件后执行,或者可以在写入之前执行

谢谢

//发布wordpress
$postID=获取_ID();
$postData=get_post($posted);
//echo$posted.'
'.$postData->post_title.
'.$postData->post_date_gmt.
; $xmlFile='/Applications/MAMP/htdocs/giraffetest/test.xml'; //加载文档 $xml=simplexml\u加载文件($xmlFile); //检查post id是否已在xml文件中-是否已设置? $nodeExists=$xml->xpath(//post[@id=“.$postID.”); //计算结果 $countNodeExists=计数($nodeExists); 如果($countNodeExists>0){//如果该ID已在文件中 //回音“我已经在这里了”; //获取正确的节点 $result=$xml->xpath(“//post[@id=“.$postID.]]/postviews”); //这里是诀窍-xpath的第一个结果和节点值(存储在[0]中) $result[0][0]=$result[0][0]+1; }否则{//如果ID不在那里,则在xml文件中为post添加一个新条目 //echo“添加ID”; $postNode=$xml->addChild('post');//向顶级节点添加新节点 $postNode->addAttribute('id',$postID);//在新 $postNode->addChild('postviews',1);//在新 } //保存更新的文档 //$xml->asXML($xmlFile); $dom=新的DOMDocument('1.0'); $dom->preserveWhiteSpace=false; $dom->formatOutput=true; $dom->loadXML($xml->asXML()); $dom->save($xmlFile);
嗨,阿希伊帕,谢谢你。代码在哪里?这是我到目前为止写的代码。您的代码需要放在这个文件的哪个位置更新答案(如果我理解正确的话)还有一个额外的问题;在loadXML行中。我已经删除了它,但我得到了以下错误…可捕获的致命错误:传递给DOMDocument::saveXML()的参数1必须是DOMNode的实例,给定字符串,在第1117行的/Applications/MAMP/htdocs/giraffetest/wp includes/theme.php中调用,并在第47行的/Applications/MAMP/htdocs/giraffetest/wp content/themes/canvas child/content-post.php中定义
//Get the wordpress postID
$postID = get_the_ID();

$postData = get_post($postID);

// echo $postID.'<br />'.$postData->post_title.'<br />'.$postData->post_date_gmt.'<br />';

$xmlFile = '/Applications/MAMP/htdocs/giraffetest/test.xml';

// load the document
$xml = simplexml_load_file($xmlFile);

// Check to see if the post id is already in the xml file - has it already been set?
$nodeExists = $xml->xpath("//post[@id=".$postID."]");

//Count the results
$countNodeExists = count($nodeExists);

if($countNodeExists > 0) { // If the ID is already in the file

        // echo 'ID already here';

        // get the correct node
        $result = $xml->xpath("//post[@id=".$postID."]/postviews");

        // heres the trick - the first result of xpath, and the node value (stored in [0])
        $result[0][0] = $result[0][0]+1;

} else { // If the ID isn;'t there, add a new entry in the xml file for the post

        //echo 'ID added';

        $postNode = $xml->addChild('post'); // adding a new <post> to the top level node
    $postNode->addAttribute('id', $postID); // adding a <postid> inside the new <post>
    $postNode->addChild('postviews', 1); // adding a postviews inside the new <post>
}

// save the updated document

//$xml->asXML($xmlFile);
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$dom->save($xmlFile);