A
A
Anton2019-02-18 14:07:22
PHP
Anton, 2019-02-18 14:07:22

How to fix Namespace prefix g on id is not defined?

I create an xml file, I need a tag, <g:id>but when using a colon, it swears at an error:

This page contains the following errors: error on line 8 at column 39: Namespace
prefix g on id is not defined

<?php
    $dom = new domDocument("1.0", "utf-8"); 
    $root = $dom->createElement("channel");
    $dom->appendChild($root);
    $item = $dom->createElement("item"); 
    $login = $dom->createElement("login", '123'); 
    $item->appendChild($login);
    $login = $dom->createElement("g:id", '456');  //ОШИБКА ТУТ-РУГАЕТСЯ НА ДВОЕТОЧИЕ
    $item->appendChild($login);
    $root->appendChild($item);
    $dom->save("users.xml");
?>

Actually, how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Homr, 2019-02-18
@anton99zel

Try this: php.net/manual/en/domdocument.createelementns.php UPD:

<?php
    $dom = new domDocument("1.0", "utf-8"); 
    $root = $dom->createElement("channel");
    $root ->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:g', 'http://base.google.com/ns/1.0');
    $dom->appendChild($root);
    $item = $dom->createElement("item"); 
    $login = $dom->createElement("login", '123'); 
    $item->appendChild($login);
    $login = $dom->createElement("g:id", '456');
    $item->appendChild($login);
    $root->appendChild($item);
    $dom->save("users.xml");
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question