I am trying to make a data export from D8 content to an XML file. Views_data_export module would be ideal for this but the XML I need to make has quite strict tag name restrictions (data import to a 3rd party system). Tag names can be changed for fields but how about the higher level <response> and <item> -tags?

I did not find any documentation about this, only instructions for D7 on how to change them. It would be interesting to know if this can be accomplished on D8 version at all at the moment? If not, this ticket could be a "Feature request" instead :)

Thanks for any help in advance!

CommentFileSizeAuthor
#50 2886357-50.patch12.35 KBChizh273
#50 Screenshot 2023-11-10 at 15.48.13.png659.23 KBChizh273
#47 2886357-47.patch12.35 KBreszli
#44 2886357-44.patch9.5 KBXperd
#42 2886357-42.patch9.73 KBXperd
#35 2886357-35.patch9.17 KBsmulvih2
#35 vde-xml-settings.jpg91.41 KBsmulvih2
#34 2886357-34.patch6.07 KBsmulvih2
#30 2886357-30.patch6.09 KBQusai Taha
#29 2886357_29.patch5.98 KBsj.suraj
#25 2886357-25.patch5.53 KBmatio89
#23 interdiff_18-23.txt685 bytesNikolay Borisov
#23 change_default_response_item-2886357-23.patch5.23 KBNikolay Borisov
#21 interdiff_15-18.txt572 bytesNikolay Borisov
#18 change_default_response_item-2886357-18.patch5.11 KBNikolay Borisov
#15 2886357-15.patch5.12 KBUpchuk
#14 2886357-14.patch3.41 KBUpchuk
#12 2886357-12.patch3.05 KBUpchuk
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Sissonen created an issue. See original summary.

tmountjr’s picture

I would like this functionality as well. I have a client taking webform submissions and translating them to an XML feed they can import into their CRM, but if I'm unable to change the "<response><item>...</item></response>" format it doesn't do them any good.

goldlilys’s picture

I need a way to display XML format with the following structure:

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>[link to xml]</link>
    <description></description>
    <language>en</language>
<slide>
	<title>[slide-title]</title>
	<startdate>July 18, 2017</startdate>
	<enddate>November 19, 2017</enddate>
	<time>11 am - 4 pm</time>	
	<location>[location here]</location>
	<description>TBD</description>
	<image>path/to/image/here</image>
</slide>
<slide>
	<title>[slide-title]</title>
	<startdate>July 18, 2017</startdate>
	<enddate>November 19, 2017</enddate>
	<time>11 am - 4 pm</time>	
	<location>[location here]</location>
	<description>TBD</description>
	<image>path/to/image/here</image>
</slide>
</channel>
</rss>

As you can see, besides being able to change the field tags, I also want to change the <item> tags to <slide> to group each item and <response> to <channel>

In D7, the xml version had the headers tags:

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>[link to xml]</link>
    <description></description>
    <language>en</language>

available ... why isn't it included in D8?

heatherwoz’s picture

I was looking for how to set or change the encoding. That would be helpful too in addition to changing the default XML tags.

aiphes’s picture

Suscribing too.

Sissonen’s picture

Category: Support request » Feature request

Changing category to feature request.

abiyub’s picture

I am trying to do the same. -- I used Views Data Export module for Drupal 7. it looks like it is not available for D8 now.

drfuzetto’s picture

I am interested in this feature as well.
D7 you could define the root and item node. This feature doesn't not exist in D8.

aiphes’s picture

Is it planned for future beta or RC release ?

drfuzetto’s picture

Michelle’s picture

I've been digging into this because I need it to be "nodes"/"node" not "response"/"item". Using the tips in #10, I made my own serializer but wasn't sure what to put in it. I was hoping to reuse as much of the core one as possible and found that you can change the rootnode (see https://www.drupal.org/project/drupal/issues/2824837#comment-12353490 ) but "item" is hardcoded deep in there.

I also found that the core XML serializer might be going away in Drupal 9: https://www.drupal.org/project/drupal/issues/2926034

I'm not sure how to write this properly so just am going to cobble something together. Would be nice to see better xml support in the module, though.

Upchuk’s picture

Status: Active » Needs review
FileSize
3.05 KB

Here is a patch that does this. It allows the following:

  • Changing the root node name
  • Changing the individual item node names
  • Wrapping the items optionally with yet another node. This allows for use cases like
Upchuk’s picture

Ah, my markup from the third bullet was lost. It allows for use cases like:

<rss>
<channel>
<item></item>
<item></item>
</channel>
</rss>
Upchuk’s picture

Oops, forgot to set the new options in the defaults. Here we go.

Upchuk’s picture

It seems I forgot in batch exporting to remove the custom nodes added via configuration from the beginning and end of batch. Now it should be better.

mchamps’s picture

Hi, I received the follow errors when I tried to patch with composer.
The issue came up with #12, #14 & #15.

- Installing drupal/views_data_export (1.0.0): Loading from cache
- Applying patches for drupal/views_data_export
https://www.drupal.org/files/issues/2021-01-16/2886357-14.patch (Changing default and tags in XML import)
Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2021-01-16/2886357-14.patch

[Exception]
Cannot apply patch Changing default and tags in XML import (https://www.drupal.org/files/issues/2021-01-16/2886357-14.patch)!

It's a great feature. Thanks !
Following !

Upchuk’s picture

The patch applies to the latest DEV version of the module, not the release 1.0.0.

Nikolay Borisov’s picture

thank you for the patch from #15, Upchuk
unforch it is not working for me ( Drupal 8.9.13, Views data export 8.x-1.x-dev)
I assume that you have checked a newer version of the XmlEncoder, where XmlEncoder::ROOT_NODE_NAME actually exists (in symfony/serializer 5 for example https://github.com/symfony/serializer/blob/5.x/Encoder/XmlEncoder.php), but in symfony/serializer 3.4 it does not exist https://github.com/symfony/serializer/blob/3.4/Encoder/XmlEncoder.php

Thats why I am using your patch with the slight change there and it works perfectly for me.

Cheers!

Status: Needs review » Needs work

The last submitted patch, 18: change_default_response_item-2886357-18.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Upchuk’s picture

@Nikolay Borisov can you please provide an interdiff so I can see what was changed? Difficult to see otherwise.

Nikolay Borisov’s picture

FileSize
572 bytes

sure @Upchuk.
Maybe you simply did it on Drupal 9. Apparently in the version 4.4 of the symfony/serializer compoenent the ROOT_NODE_NAME constant exists -> https://github.com/symfony/serializer/blob/4.4/Encoder/XmlEncoder.php (apparently introduced in version 4.2 of the component), where the version that is coming with Drupal 8 it is missing (simply check your version in the codebase where it is working with "composer show symfony/serializer" - in my case it was v3.4.47)

Cheers!

Upchuk’s picture

+++ b/src/Plugin/views/style/DataExport.php
@@ -322,7 +322,7 @@
+      $context['xml_root_node_name'] = $options['root_node_name'];

I would check if the constant is defined and do a fallback if not. Also, not sure why the tests are failing now.

Nikolay Borisov’s picture

@Upchuk I think it is a very good idea!
Here is a patch for that.

sakonn’s picture

Hello,
the patch seems to work for me. Would like to have this functionality in this module.

matio89’s picture

Status: Needs work » Needs review
GuillaumeDuveau’s picture

Status: Needs review » Reviewed & tested by the community

Seems good to me, thanks. Tested on Drupal 9.2 + Views data export 8.x-1.0

sj.suraj’s picture

Title: Changing default "<response>" and "<item>" tags in XML import » Changing default "<response>" and "<item>" tags in XML export
Status: Reviewed & tested by the community » Needs work
Issue tags: +Need tests, +schema change

I am testing on Drupal 9.2 + Views data export 8.x-1.0
Only #25 is working for me but it's getting failed in test also it has missing schema for xml_settings, root_node_name, item_node_name, items_wrapper_node

sj.suraj’s picture

Qusai Taha’s picture

FileSize
6.09 KB

Re-roll the patch

Qusai Taha’s picture

Status: Needs work » Needs review
Qusai Taha’s picture

smulvih2’s picture

Issue summary: View changes
smulvih2’s picture

Fixed formatting issue from patch #30. Also fixed #title attribute for item_node_name which was placed under items_wrapper_node as a duplicate #title.

smulvih2’s picture

The patch in #34 isn't enough to meet my requirements, I still need some more options. Please see desired XML output:

<?xml version="1.0"?>
<recall lang="en" xmlns="urn:recall-schema">
	<product_name>Thrifty Kitchens brand Ancient Whole Grains Salad recalled due to presence of stones</product_name>
	<date>2020-03-11</date>
	<id>63475</id>
	<country_id>CA</country_id>
	<product_type>Food</product_type>
</item>

What is missing:

  • My exports are only for a single entity. I am using a view with contextual filter using nid. I need to remove the root node element so the item node becomes the root node.
  • I need to be able to add attributes to the "root" node, in this case the item node.
  • I need to be able to remove empty fields so if no data is available the element is not returned in XML.

New patch implements the required changes above, while still supporting requirements from previous comments. I tested this with both standard and batch methods and works as expected with any combination of the options enabled.

New XML options:

VDE XML options form

GuillaumeDuveau’s picture

Patch in #35 works for me, thanks!

tarasiadis’s picture

Patch in #35 works for me too, thanks!

But how can I set <?xml version="1.0" encoding="UTF-8" ?>
Maybe one more parameter as Root node attributes for xml element parameters?

nojj’s picture

Patch #35 works for even for the 8.x-1.1 release.
but what about this

PHP 7.4 & MySQL 5.7, D9.3 Skipped, branch did not pass for 4 weeks

?

aiphes’s picture

#35 add extra settings but xml output by default is like:

<?xml version="1.0"?>
<response><item/></response>

How can I add this infos ?:

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

Thanks

Xperd made their first commit to this issue’s fork.

Xperd’s picture

Added XML prolog encoding="utf-8" to patch #35

tarasiadis’s picture

I have problem to apply patch #42

Get below messages....
Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2022-09-25/2886357-42.patch

[Exception]
Cannot apply patch Ρυθμίσεις παραμέτρων της εξαγωγής xml για το root-item (https://www.drupal.org/files/issues/2022-09-25/2886357-42.patch)!

Xperd’s picture

FileSize
9.5 KB
tarasiadis’s picture

Thanks Xperd, #44 patch works.

One more issue. The produced file is not optimized in tree structure (beautifier style) like other xml files. How can we achieve this?

tarasiadis’s picture

Sorry styling of tree I think is problem of my editor as on Chrome works ok.

reszli’s picture

changes to improve functionality and code:

  • I needed some more elements inside the root node, so I introduced an additional config field called "metadata" as a textarea
  • also, I replaced the custom replacement of {{ lang }} by allowing the use of token in the attributes and metadata fields
  • additionally I switched to dep. inj. where possible

still left to do:

  • instead of strpos and str_replace, switch to a better way of manipulating the XML (i.e. using DOMDocument)
jhedstrom’s picture

Version: 8.x-1.0-alpha4 » 8.x-1.x-dev
Status: Needs review » Needs work

The code in #47 and in the MR need to be reconciled. It would also be great to add a batch export test for these custom xml tags.

Chizh273’s picture

The #47 has one issue.
If you open the settings of the data export format with enabled warning logging ($config['system.logging']['error_level'] = 'verbose';) you will get a warning "Warning: Undefined array key "metadata" in /var/www/html/web/modules/contrib/views_data_export/src/Plugin/views/style/DataExport.php on line 241".

Screenshot

I have updated the #47 patch to fix this warning.

sir_squall’s picture

Thank you very much the path #50 work well!

proweb.ua’s picture

#50 works

how to add attributes to Item node name?
can this be done via the API?