从HTML创建XML并添加到现有XML表[重复]

时间:2022-10-20 15:48:06

This question already has an answer here:

这个问题在这里已有答案:

So in Drupal i am looking to create an XML form using code for each node. I have successfully created an XML table using the following code...

所以在Drupal中我希望使用每个节点的代码创建一个XML表单。我使用以下代码成功创建了一个XML表...

$xml = new DOMDocument("1.0", "utf-8");
$root = $xml->createElement("article");
$xml->appendChild($root);
$front = $xml->createElement("front");
$root->appendChild($front);
$journal_meta = $xml->createElement("journal-meta");
$front->appendChild($journal_meta);
$journal_id = $xml->createTextNode($form['#node']->field_journal_id['und'][0]['value']);
$journal_meta->appendChild($journal_id);

$xml->formatOutput = true;
echo "<xmp>". $xml->saveXML() ."</xmp>";
$xml->save("mybooks.xml") or die("Error");

OUTPUT:
<?xml version="1.0" encoding="utf-8"?>
<article>
  <front>
    <journal-meta>en</journal-meta>
  </front>
</article>

Now i also want to load the body field which includes HTML and add this to the XML table output above. I have managed to load the body field into a domDocument and then specifically targeted the body tag to create the node list.

现在我还想加载包含HTML的body字段,并将其添加到上面的XML表输出中。我已设法将body字段加载到domDocument中,然后专门针对body标记创建节点列表。

What i have not been able to do is iterate over the node list, and all all the elements and appendchilds on my original $xml variable.

我无法做的是遍历节点列表,以及我原始的$ xml变量上的所有元素和appendchild。

$body = $form['#node']->body['und'][0]['value'];
$tidy = tidy_parse_string($body);
$body = $tidy->body();
$dom = new DomDocument();
$dom->loadHTML('<?xml encoding="UTF-8">' . $body);

print_r($dom->saveHTML());
$div = $dom->getElementsByTagName('body');

The $dom variable contains the domDocument and $div variable contains the Nodelist, how can this be done to create one XML document without using Cdata?

$ dom变量包含domDocument,$ div变量包含Nodelist,如何在不使用Cdata的情况下创建一个XML文档?

1 个解决方案

#1


0  

You can use importNode, importXml but that is the though way to go. Form the code you provide the issue I see you are using two DomDocuments. Use only one, both for creating the farme and the html. Save in the end

您可以使用importNode,importXml,但这是可行的方法。形成您提供的代码,我发现您使用的是两个DomDocuments。只使用一个,用于创建farme和html。保存到底

$content = array(
    'article' => array(
        'front' => array(
            'journal-meta' => 'your-content-string'
        )
    )
);

$encoded = json_enocde($content);
$decoded =  json_decode($encoded); // associative array again

#1


0  

You can use importNode, importXml but that is the though way to go. Form the code you provide the issue I see you are using two DomDocuments. Use only one, both for creating the farme and the html. Save in the end

您可以使用importNode,importXml,但这是可行的方法。形成您提供的代码,我发现您使用的是两个DomDocuments。只使用一个,用于创建farme和html。保存到底

$content = array(
    'article' => array(
        'front' => array(
            'journal-meta' => 'your-content-string'
        )
    )
);

$encoded = json_enocde($content);
$decoded =  json_decode($encoded); // associative array again