Diferencia entre revisiones de «Gambas/Manipular documentos XML»

Contenido eliminado Contenido añadido
Página creada con ' == '''Estructura básica de un documento XML''' == == '''Escribir XML''' == == '''Leer XML''' == == '''Usar XSLT''' =='
 
Sin resumen de edición
Línea 1:
Segun la W3C: ''"XML es un simple y muy flexible formato de texto derivado de SGML (ISO 8879). Originalmente diseñado para cumplir los retos de gran escala de publicación electrónica. XML también juega un rol de creciente importancia en el intercambio de una gran variedad de datos en internet y otros lugares"''
Y no estan exagerando. Por ejemplo, el formato XHTML usado en internet es una aplicación de XML, y puedes encontrar aplicaciones de XML en casi cualquier intercambio de datos. En estos días, XML es una parte esencial del repertorio de un programador completo. Gambas provee casi todo lo que necesitas para trabajar con XML.
{{Consejo | Es importante aclarar que XHTML es un documento tipo XML pero no son lo mismo. XHTML esta limitado a paginas de internet como su formato estándar. Sin embargo, XML es un formato de propósito general y puede ser usado para trabajar con cualquier tipo de datos}}
 
== '''Estructura básica de un documento XML''' ==
All XML documents start with <?xml version=”1.0” encoding=”UTF-16”?> which indicates that the rest of the document contain data in XML format, specify what version is being used and the Unicode character encoding.
 
On version 1.0 you can skip to include the XML declaration, but for version 1.1 it is mandatory.
The XML declaration is followed by a ‘root’ element that can contain any number of sub-elements between their start-tag and end-tag, elements can contain attributes, and attribute names may appear only once in any single element. Elements must be properly nested, may never overlap, and so must be closed in the order opposite to which they were opened. XML comments start with <!-- and end with -->. With this in mind, take a look of the following small well-formed XML document.
<?xml version=”1.1” encoding=”UTF-16” ?>
<!-- The previous line is the XML declaration -->
<!-- This line is a comment -->
<root>
<element_name attribute_name=”attribute_value”>
Element Content
</element_name>
</root>
As you can see, the document is separated in different lines and there are some indentation to indicate the different levels of nesting, however this is not necessary the whole thing can be in just one text line. It is formatted in this way to help you understand the document. You’ll find this format useful when you need to read or edit a XML document on a text editor.
 
== '''Escribir XML''' ==
You’ll write a program that must create the following XML document based on one of the classes you were working on back in the Objects lesson.
 
<?xml version=”1.0” encoding=”UTF-16” ?>
<!-- Heroes Characters -->
<characters serie=”Heroes”>
<heroe id=”1” name=”Claire Bennet”>
<name>Claire Bennet</name>
<played_by>Hayden Panettiere</played_by>
<ability>
Rapid cellular regeneration
</ability>
</heroe>
<heroe id=”2” name=”Hiro Nakamura”>
<name>Hiro Nakamura</name>
<played_by>Masi Oka</played_by>
<ability>
Space-time manipulation: teleportation & time travel
</ability>
</heroe>
<villain id=”1” name=” Gabriel Sylar”>
<name>Gabriel Sylar</name>
<played_by>Zachary Quinto</played_by>
<ability>
Understand how things work and multiple other abilities acquired
</ability>
</villain>
</characters>
Start a new command-line application named WriteXML, just make sure to check the XML/XSLT programming option on the New Project – Project Type window. By doing this, you let Gambas know the gb.xml and gb.xml.xslt need to be included as components of the project. Or if you forgot to select the XML/XSLT option you can include it by selecting the gb.xml component on Project\Properties…\Components. Then you’ll create on the MMain function a XmlWriter object that will be opened to write and saved on your home directory as Heroes.xml and will be generated with the helpful indentation we discussed earlier.
Dim writer as XmlWriter
writer = NEW XmlWriter
writer.Open(User.Home & “/Heroes.xml”, TRUE)
writer.Comment(“Heroes Characters”)
‘Following code goes here
writer.EndDocument()
The file opened with Open is not actually written to the user home folder until the EndDocument method is called. This method, also add any end-tag missing in order to insure compliance with the XML guidelines.
The root element is character, and to indicate Gambas this you’ll write the following code inside the Open and EndDocument lines:
writer.StartElement(“characters”)
writer.Attribute(“serie”, “Heroes”)
‘Elements code will replace this comment line
writer.EndElement ‘characters
The Oracle used to say “everything that has a beginning has an end” and here for XML is absolutely true. That is why you wrote the EndElement method right after its corresponding StartElement. It is a good practice to do this every time you write an open statement that needs to be closed lately, especially to avoid debug headaches.
Inside the StartElement and EndElement methods, you’ll write the first heroe element.
writer.StartElement(“heroe”)
writer.Attribute(“id”, “1”)
writer.Attribute(“name”, “Claire Bennet”)
writer.StartElement(“name”)
writer.Text(“Claire Bennet”)
writer.EndElement ‘name
writer.StartElement(“played_by”)
writer.Text(“Hayden Panettiere”)
writer.EndElement ‘played_by
writer.StartElement(“ability”)
writer.Text(“Rapid cellular regeneration”)
writer.EndElement ‘ability
writer.EndElement ‘heroe 1
Right after you’ll write the code for the second heroe element with some shortcuts that you’ll love.
writer.StartElement(“heroe”, [“id”, “2”, “name”, “Hiro Nakamura”])
writer.Element(“name”, “Hiro Nakamura”)
writer.Element(“played_by”, “Masi Oka”)
writer.Element(“ability”, “Space-time manipulation: teleportation & time travel”)
writer.EndElement ‘heroe 2
Run the application and open the Heroes.xml file with a plain text editor or with the web browser to see the resulting XML document.
 
== '''Leer XML''' ==