From 1109132f09d75da9a28b649c7677bb6ce07c40c0 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:41:45 -0500 Subject: initial commit --- .../guide/parsing_and_serializing_xml/index.html | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 files/es/web/guide/parsing_and_serializing_xml/index.html (limited to 'files/es/web/guide/parsing_and_serializing_xml/index.html') diff --git a/files/es/web/guide/parsing_and_serializing_xml/index.html b/files/es/web/guide/parsing_and_serializing_xml/index.html new file mode 100644 index 0000000000..faff8bfbe4 --- /dev/null +++ b/files/es/web/guide/parsing_and_serializing_xml/index.html @@ -0,0 +1,133 @@ +--- +title: >- + Convertir código a cadena de texto (serializing) y visceversa (parsing) a un + XML +slug: Web/Guide/Parsing_and_serializing_XML +translation_of: Web/Guide/Parsing_and_serializing_XML +--- +

La plataforma web proveé Los siguientes objetos para hacer parsing (convertir una cadena de texto a código) y serializing (visceversa) a un XML:

+ + + +

Parte 1: Como crear un documento XML 

+ +

Usar una de la siguientes opciones para crear un documento XML  (el cual es una instancia de Document).

+ +

Codificando textos al árbol del DOM

+ +
+
var miTexto = '<a id="a"><b id="b">Hey!</b></a>';
+var codigo = new DOMParser();
+var oDOM = codigo.parseFromString(miTexto, "text/xml");
+// Imprimir el nombre del elemento raiz o un mensaje de error
+dump(oDOM.documentElement.nodeName == "parsererror" ? "error mientras se codificaba" : oDOM.documentElement.nodeName);
+
+
+ +

Creando un documento XML empezando desde un árbol de Objetos JavaScript (JXON)

+ +

Por favor vea JXON algoritmos de reversa.

+ +

Codificando rexursos de URL direccionables en árboles del DOM

+ +

Usando XMLHttpRequest

+ +

Aquí hay un código de ejemplo que lee y codifica un archivo XML con  URL direccionable en un árbol del DOM:

+ +
var xhr = new XMLHttpRequest();
+xhr.onload = function() {
+  dump(xhr.responseXML.documentElement.nodeName);
+}
+xhr.onerror = function() {
+  dump("Error mientras se tomaba el XML.");
+}
+xhr.open("GET", "example.xml");
+xhr.responseType = "document";
+xhr.send();
+
+ +

xhr.responseXML es una instancia de {{domxref("Document")}}.

+ +

Parte 2: Como serializar el contenido de un documento XML

+ +

Use the following approaches to serialize the contents of the XML document you created in Part 1.

+ +

Serializing DOM trees to strings

+ +

First, create a DOM tree as described in How to Create a DOM tree. Alternatively, use a DOM tree obtained from {{ domxref("XMLHttpRequest") }}.

+ +

Now, let's serialize doc — the DOM tree — to a string:

+ +
var oSerializer = new XMLSerializer();
+var sXML = oSerializer.serializeToString(doc);
+ +

The new XMLSerializer() constructor is not available from within a JS XPCOM component (or a JS module). Instead, write:

+ +
var oSerializer = Components.classes["@mozilla.org/xmlextras/xmlserializer;1"]
+                            .createInstance(Components.interfaces.nsIDOMSerializer);
+var sXML = oSerializer.serializeToString(doc);
+
+ +

"Pretty" serialization of DOM trees to strings

+ +

You can pretty print a DOM tree using XMLSerializer and E4X. First, create a DOM tree as described in the How to Create a DOM tree article. Alternatively, use a DOM tree obtained from {{ domxref("XMLHttpRequest") }}. The doc variable contains the DOM tree.

+ +
var oSerializer = new XMLSerializer();
+var sPrettyXML = XML(oSerializer.serializeToString(doc)).toXMLString();
+ +

Indents consist of two spaces. To write a more efficient version or customize the indent string, use {{ domxref("treeWalker") }}.

+ +
Note: When using the E4X toXMLString method, your CDATA elements will be lost, and only the containing text will remain. So if you have CDATA elements in your XML, using the preceding method might not be useful.
+ +
<content><![CDATA[This is the content]]></content>
+
+ +

Becomes

+ +
<content>This is the content</content>
+ +

Serializing DOM trees to Javascript Object trees (JXON)

+ +

JXON (lossless JavaScript XML Object Notation) is a way to represent JavaScript Objects using XML. To address only parts of an XML document, use XPath instead of converting the whole document into JSON! Otherwise, read the article about JXON.

+ +

Serializing DOM trees to files

+ +

First, create a DOM tree as described in the How to Create a DOM tree article. If you already have a DOM tree from using {{ domxref("XMLHttpRequest") }}, skip to the end of this section.

+ +

Now, let's serialize doc, the DOM tree, to a file. For more information about files, see about using files in Mozilla):

+ +
var oFOStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
+var oFile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsILocalFile); // get profile folder
+oFile.append("extensions"); // extensions sub-directory
+oFile.append("{5872365E-67D1-4AFD-9480-FD293BEBD20D}"); // GUID of your extension
+oFile.append("myXMLFile.xml"); // filename
+oFOStream.init(oFile, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
+(new XMLSerializer()).serializeToStream(doc, oFOStream, ""); // rememeber, doc is the DOM tree
+oFOStream.close();
+
+ +

Serializing XMLHttpRequest objects to files

+ +

If you already have a DOM tree from using {{ domxref("XMLHttpRequest") }}, use the same code as above but replace serializer.serializeToStream(doc, oFOStream, "") with serializer.serializeToStream(xmlHttpRequest.responseXML.documentElement, oFOStream, "") where xmlHttpRequest is an instance of XMLHttpRequest.

+ +

Note that this first parses the XML retrieved from the server, and then re-serializes it into a stream. Depending on your needs, you could just save the xmlHttpRequest.responseText directly.

+ +

Serializing HTML documents

+ +

If the DOM you have is an HTML document, you can serialize it simply using

+ +
var serialized = document.documentElement.innerHTML;
+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf