-1

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...

$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.

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.

$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?

Key
  • 396
  • 4
  • 15
  • Could you provide an example of the target xml? – ThW Jun 10 '14 at 13:39
  • The aim is to create an XML feed from a page. if you mean an example of the body text that i am trying to add, it is basically any html.

    this is some test content

    more test content

    – Key Jun 10 '14 at 15:14
  • Not the body, but a full, complete XML you would like to generate (with a minimum of HTML). *btw* I prefer Atom for feeds, sometimes extended with my own namespace for specific data. – ThW Jun 10 '14 at 15:43
  • Please use the search before asking a quesiton. Also please reduce your examples by creating new one from scratch only highlighting an isolated issues with as little code as necessary. Do not post your development code. Try to reproduce your own problems, that helps a lot, also in finding a solution (not only asking a question). – hakre Jun 10 '14 at 20:16

1 Answers1

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

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

$encoded = json_enocde($content);
$decoded =  json_decode($encoded); // associative array again
Sesertin
  • 462
  • 2
  • 11
  • aha yes perfect almost has me at what i am looking for. Just off that how can i get the $div variable to be stored in the actual XML. That is being saved as a Nodelist and i need to strip the HTML tags from the XML output. – Key Jun 10 '14 at 11:39
  • Aaaa, are you actually looking to produce an html page, or to store contents in an xml format? Maybe you can just use php striptags() function? – Sesertin Jun 10 '14 at 11:45
  • I will look into to the striptags() later. For now yes i am trying to store all content in XML format. I want to define the first lot of XML shown in the initial code, and then load the HTML from the body field. After defining some element and child nodes...and then running $xml->loadHTML, this overrides all my initial content. I then thought above rearranging but apparently XML cannot be rearranged. So basically i want to define some elements, and then do the loadHTML call without overriding all my content. – Key Jun 10 '14 at 12:10
  • Believe me, you don't want to use xml. Just create an associative array and store it as a json string. Faster, no need for Domdocument, no need for xml parser. Shorter, easier. I updated my original answer with an example for you. – Sesertin Jun 10 '14 at 12:17
  • The requirement is to create an XML feed for each book page, would i still be able to create an XML feed whilst storing in a JSON string? – Key Jun 10 '14 at 15:16
  • No, than its not your way to go. I thought you just simply need some structured storage and json is great way to do this. If you need to create an XML feed than there's no point in bringing json into the picture. – Sesertin Jun 10 '14 at 15:24
  • @user3718333 Also, your whole advice about not using XML is plain wrong and misguiding. There is nothing wrong with XML, it is more powerful in quite a few ways in comparison to JSON and it is _everywhere_. Don't think that everything is a web 2.0 application and everyone uses JSON - They don't in the professional world. – dirkk Jun 10 '14 at 19:37
  • @dirkk Yes, nothing is wrong with XML except for every single structure you have to define a creation and a reader process (unless you manage to use the very same structure). Just about the professional world: Fiefox, and Chrome are all switching to use json instead of XML with storing bookmarks and user data. After all neither is wrong, both are for storing structured data in string format and/or passing between applications. I personally find json a hell a lot of simpler, but that does not mean that using XML would be wrong.Anyway I would be interested in what way you fund XML more powerful? – Sesertin Jun 11 '14 at 12:08
  • @user3718333 You are right, both are valid data storing and exchange formats. That's why I am always baffled when someone suggest instantly JSON, althouht not knowing any requirements. I have no idea what you mean with a creation and reader process and they are certainly not required. I use XML on a daily basis with XPath/XQuery and have never used what I could imagine be a reader process. XML is more powerful in some regards, e.g. namespace support or schema validation. – dirkk Jun 11 '14 at 12:26