I need to build a sitemap.xml with a REST export with a view. By default I get this data structure with a REST export.

<?xml version="1.0"?>
    <item key="0">

How can I modify the tags of this structure? I need to get this tags.

<?xml version="1.0" encoding="UTF-8"?>
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

These tags must be changed.

<response> > <urlset...
<item> > <url>

Do I write a custom serializer for the REST export or a specific encoder? How do I proceed?


In D8 you need to create custom serializer and encoder to alter the XML tags.

  1. Create custom serializer Ref : https://gist.github.com/BERRAMOU/dd21e009eec0e3d98733e78360122892
  2. Write custom encoder


namespace Drupal\custom_serializer;
use Drupal\serialization\Encoder\XmlEncoder as SerializationXMLEncoder;
use SimpleXMLElement;
 * Encodes xml API data.
 * @internal
class myXMLEncoder extends SerializationXMLEncoder {

   * The formats that this Encoder supports.
   * @var string
  protected static $format = ['myxml'];

   * {@inheritdoc}
  public function getBaseEncoder() {
    $base_encoder = parent::getBaseEncoder();
    return $base_encoder;
  1. Then in View under 'FORMAT' you will see both options.
  2. After save, you should get XML with required tag.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.