Create node in xml with jquery

Asked

Viewed 310 times

2

Hello, guys, I have a problem: I’m trying to create a node in an xml with jquery and I can’t... I have an xml file which I get it with ajax:

<?xml version="1.0" encoding="UTF-8"?>
<dados>
    <usuarios>
        <usuario>
            <login>usuario</login>
            <senha>senha123</senha>
        </usuario>
    </usuarios>
</dados>

I can read user nodes easily. But I’m not able to add another user. Can anyone help me? If there is any form with pure javascript there is also no problem. Thanks already!

Edit 1: The jquery code I’m using is this:

$.ajax({
        url: "../xml/dados.xml",
        type: "get",
        dataType: "xml",
        success: function(data) {
            xml = data;
            var xmlDoc = $(xml);
            var element = xmlDoc.filter('usuarios');
            $(element).attr('type', 'vip');
            $(element).append("<usuario><login>usuario2</login><senha>senha321</senha></usuario>");
            $("body").append(xmlDoc);
        }
});

I found this looking code on this jquery forum: https://forum.jquery.com/topic/adding-xml-nodes-to-an-xmldocument-with-jquery-1-5

  • You could post the javascript code you are using to try to add user?

  • I will edit the post

2 answers

1


you can manipulate a XML in the same way that it manipulates a HTML.

then you just need to make a dial that got the knot usuarios and add content to it.

var xmlText = `<?xml version="1.0" encoding="UTF-8"?>
  <dados>
      <usuarios>
          <usuario>
              <login>usuario1</login>
              <senha>senha123</senha>
          </usuario>
      </usuarios>
  </dados>
`;

var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlText, "text/xml");

var xml = $(xmlDoc);
var usuarios = $("usuarios", xmlDoc);

usuarios.append(`
  <usuario>
      <login>usuario2</login>
      <senha>senha321</senha>
  </usuario>
`);

console.log(xmlDoc.documentElement.outerHTML);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

the code displayed above using Javascript only.

var xmlText = `<?xml version="1.0" encoding="UTF-8"?>
  <dados>
      <usuarios>
          <usuario>
              <login>usuario1</login>
              <senha>senha123</senha>
          </usuario>
      </usuarios>
  </dados>
`;

var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlText, "text/xml");
var usuarios = xmlDoc.documentElement.querySelector("usuarios");

var usuarioText = `<?xml version="1.0" encoding="UTF-8"?>
  <usuario>
      <login>usuario2</login>
      <senha>senha321</senha>
  </usuario>
`;

var usuarioDoc = parser.parseFromString(usuarioText, "text/xml");
usuarios.appendChild(usuarioDoc.documentElement);

console.log(xmlDoc.documentElement.outerHTML);

  • Oops! For me it worked this second way! Thank you!

0

See if that solves your problem

$.get('teste.xml', function(response){
			var xml = response;
			var usuarios = xml.querySelector('usuarios');
			var novoUsuario = xml.createElement('usuario');
			var login = xml.createElement('login');
			var senha = xml.createElement('senha');

			login.appendChild( xml.createTextNode("geeksilva") );
			senha.appendChild( xml.createTextNode("123") );
			novoUsuario.appendChild(login);
			novoUsuario.appendChild(senha);
			usuarios.appendChild(novoUsuario)


		}, 'xml');	

Feito usando javascript puro mesmo, veja que basta usar o DOM que se usa normalmente para o Htmldocument. The difference is the object that is Xmldocument but the way to manipulate the DOM follows the same principles

Browser other questions tagged

You are not signed in. Login or sign up in order to post.