Working with XML in .NET applications. How to work with XML - review of online services and xml editors


Quite a long time has passed since I promised to talk about working with data in XML format when developing .NET applications. Promises must be kept. Please note that this article is not aimed at professional .NET developers, but at those who do not yet have significant experience in creating .NET applications.


Why should we work with XML?

If you don’t yet have a very good idea of ​​what XML is, then I recommend reading the article “XML is serious and for a long time” in “KV” No. for 2007. To save space for more important things, the XML format itself will not be analyzed here.

Before you start getting acquainted with the program code, you need to answer one fundamental question: why even include XML support in your application? I think many people have asked themselves this question regarding many technologies, and not all of them were included in applications after the answer. However, with XML the situation is somewhat different, and there are real reasons to use this format in many situations.

The main advantage of XML is that, being a text format by nature, it nevertheless perfectly allows storing and transmitting any data. Since this format is text, the problem of its cross-platform is solved by itself, and the problem of transmitting XML data is just as easily solved (as is, for example, done in SOAP). In addition, you can easily change the data transfer or storage format by adding new attributes and without worrying too much about compatibility with previous versions of the format, since applications using the old version will be able to read what they need without paying attention to new tags or attributes . Many applications use XML-based file formats, many data transfer protocols are also based on XML, and the lists of both continue to grow.

Of course, on the other hand, XML is not very economical, because, as one of the visitors to the Computer News forum once correctly noted, XML documents often consist of 10% data and 90% tags. However, this largely depends on what tags you choose. You can write st. Melnikaite, 2, Can i . Although, to be honest, it seems to me that with the current hard drives and thick channels there is no point in being particularly compressed.

So in the right hands, XML is a powerful and convenient thing, and due to the ubiquity of this format, you can’t escape it at all. So let's move on to writing program code.

For programming we will use the main language of the .NET platform - C#. In order for as many readers as possible to practice with the given program code themselves, I will use the first version of C# and the .NET Framework.


Data recording

First, let's talk about writing data, because, you know, in order to read something from somewhere, you must first write something somewhere. And since you and I started programming, it’s not appropriate for us to create XML data manually. So let's first get started with writing the data into XML.

First, create a new project in Visual Studio, #Develop or C# Builder, and add System.Xml to the list of imported namespaces.

A special class, XmlTextWriter, is responsible for writing XML data in the .NET Framework, which allows you to write XML data to an arbitrary stream. That is, we, generally speaking, can use it to write it to a file, a database, and send to someone over the Internet, but now we will write everything to a file. You can redirect the output by changing the object constructor (i.e., passing not the file name and its encoding during initialization, but an object that is a data stream). However, I think I'm getting a little ahead of myself, let's first take a look at the code responsible for writing data to our XML file.

String FileName = "c:\\demo.xml"; XmlTextWriter xml = new XmlTextWriter(FileName, System.Text.Encoding.Unicode); xml.Formatting = Formatting.Indented; xml.WriteStartDocument(); xml.WriteStartElement("rootelement"); for (int i = 0; i< 10; i++) { xml.WriteStartElement("subelement"); xml.WriteAttributeString("attrib1", "value1"); xml.WriteAttributeString("attrib2", i.ToString()); for (int j = 0; j < 10; j++){ xml.WriteStartElement("subsubelement"); xml.WriteAttributeString("attr", j.ToString()); xml.WriteEndElement(); } xml.WriteEndElement(); } xml.WriteEndElement(); xml.WriteEndDocument(); xml.Close();

The first line, I think, is quite clear - it’s simply recording the name of the file into which we will save the data. Next, we create an object of type XmlTextWriter (it is called, as you can see, xml), and it is with it that we will perform all further operations. Please note that when constructing an object, we also specify the encoding in which the XML will be written: in our example, this is Unicode. The next line, generally speaking, is not required, but it will make our XML document, as they say, human readable, that is, it will add the necessary indents and break it into lines. Without this, the entire document would be written in one line, which, although it saves space, makes it practically unsuitable for manual editing.

Document writing begins by calling the WriteStartDocument() method of our xml object. The line following it adds the root element “rootelement” to our XML document (let me remind you that for XML documents the root element must be present in a single copy). Next, in a cycle, we add ten more elements that do not carry any semantic load to our XML document, for each of which we set two attributes and ten more subelements. Please note that we can add a number to a string without explicitly converting the data, but if the number must entirely form a string, then it must be converted explicitly using the ToString() method. Also note that we must explicitly close each of the elements of our XML document, and then the entire document.

Now that our XML document has been successfully written, let's see how we can read data from it.


Reading data

Add a listBox component to the form of your application (unless, of course, it is a console application) so that you can monitor the result of reading the XML file. Well, if your program is a console program, then you can easily redirect the output to the console.

As usual, let's first get acquainted with the program code, and then we will look at what exactly this code does.

XmlTextReader xml = new XmlTextReader(FileName); xml.WhitespaceHandling = WhitespaceHandling.None; int i = 0; while (xml.Read())( if ((xml.NodeType == XmlNodeType.Element) & (xml.Name == "subelement")) ( listBox1.Items.Add("subelement " + i + " found") ; i++; listBox1.Items.Add(" " + xml.GetAttribute("attrib1")); listBox1.Items.Add(" " + xml.GetAttribute("attrib2")); while (xml.Read()&( xml.Name == "subsubelement"))( listBox1.Items.Add(" " + xml.GetAttribute("attr")); ) ) ) xml.Close();

For reading, as you may have noticed, we use another class, namely XmlTextReader. It is in the same namespace as the class we used to write the data. In the first line, we create an instance of XmlTextReader, named xml (here we assume that the FileName variable was already defined by us earlier). To skip empty lines if they somehow inexplicably appear in our just created XML file, we use the following line in the code snippet below.The i variable is used to count the number of "subelement" elements found in the XML file from which the data is being read.

Next comes the cycle of directly reading data from the file. The Read() method reads the next element of the XML document, and after reading it, we check what exactly we read. If it is indeed a "subelement" element, then we add information about the element read to listBox1, increment a variable containing the number of elements read, and then read the element's attributes. After reading the attributes, we organize a separate loop to read the subelements (note that we do not need a separate XmlTextReader for this) and the attributes of these subelements. As before, we enter all the information read into listBox1 to control the correctness of reading.

When reading XML files, in the same way as when writing them, when constructing an XmlTextReader, you can specify as a parameter the stream from which to read, and then you can read not only from files, but also from other sources , examples of which I have already given above.One useful feature of the XmlTextReader class should be noted: when reading, it does not load the entire XML document being read into memory, so it is convenient to parse large XML documents (for example, XML databases).


Behind the scenes

In general, the example we just discussed is too simple for real projects. Nowadays, when reading XML documents, they are usually validated using DTDs, XML Schema or Relax NG. Validation is checking that the markup of an XML document conforms to some standard described in external file. Validation is needed so that document verification is not hard-wired into the program algorithm, but can be changed arbitrarily when the data format changes without updating the program code that reads or writes data. Unfortunately, now we won’t have time to sort out the validation, because, as you yourself understand, the volume of a newspaper article has certain limitations.

Another interesting and useful practical point regarding working with XML data is XSL data transformation. This transformation is applied to data when it is displayed on HTML pages and, in fact, is simply applying a specific web page template to an XML file containing some data. Since the lion's share of current use of XML data is in one way or another World Wide Web, then it would be very, very good to consider XSL transformations.

So, I think, this article will have a continuation - but, of course, only if you yourself (that is, the readers of Computer News) ask about it on the forum or in a letter to my email address. For now, that’s probably all about using XML in .NET applications. I hope this information is useful to you.

XML support first appeared in SQL Server 2000 - in T-SQL language were added keywords FOR XML and OPENXML, which allowed developers to, respectively, retrieve the results of database queries as an XML stream and store XML documents in the database. These capabilities have been significantly expanded in SQL Server 2005 with the introduction of a new type XML data, which supports XSD_schema-level validation, XQuery_operations, and indexing. In SQL Server 2008, the ability to work with XML as a built-in data type has been further expanded.

Let's start with a quick recap key features for working with XML, implemented in previous versions SQL Server - SQL Server 2000 and SQL Server 2005.
As I noted above, SQL Server 2000 added the FOR XML and OPENXML keywords to T-SQL. FOR XML is an attribute of the SELECT command that specifies that the results of the query should be represented as an XML stream. An example of using this functionality is shown below. Next request:

SELECT ProductID, ProductName

FROM Products

FOR XML AUTO

will return the following XML document:

< Product ProductID = "1" ProductName = "Widget" />

< Product ProductID = "2" ProductName = "Sprocket" />

The OPENXML function is designed to do the opposite - create a record based on the XML document passed to it. An example of using this functionality is shown below. Next request:

DECLARE @doc nvarchar(1000)

SET @doc = "

"

DECLARE @xmlDoc integer

EXEC sp_XML-preparedocument @xmlDoc OUTPUT , @doc

SELECT * FROM

OPENXML (@xmlDoc, "Order/Item" , 1)

WITH

(OrderID integer "../@OrderID" ,

ProductID integer

Quantity integer)

EXEC sp_XML-removedocument @xmlDoc

will create an entry like this:

OrderID ProductID Quantity

1011 1 2

1011 2 1

Note the use of the sp_XML-preparedocument and sp_XML-removedocument stored procedures to create the XML document in memory and remove it after it is written to the database.

In SQL Server 2005, the FOR XML attribute was expanded to include new options for document root elements and element names, support for nested query calls with FOR XML, which allows you to create complex hierarchies within XML documents, and a new PATH mode, which allows you to describe the structure By the resulting XML document using XPath syntax. An example of using this functionality is shown below. Next request:

SELECT ProductID AS "@ProductID" ,

ProductName AS "ProductName"

FROM Products

FOR XML PATH("Product"), ROOT("Products")

will create an XML document like this:

Widget

Sprocket

In addition to enhancements to functionality first introduced in SQL Server 2000, SQL Server 2005 introduces a built-in XML data type that allows you to create variables and columns to store XML data. An example of using this functionality is shown below.

CREATE TABLE SalesOrders

OrderDate datetime,

CustomerID integer

OrderNotes xml)

The xml data type can be used to store formatted documents (HTML, XML, XHTML, etc.) or semi-structured data in a database. You can store untyped or typed XML data - the latter can be validated against the XSD schema. To specify the schema used to validate input data, use the CREATE XML SCHEMA COLLECTION command:


Once a schema is described, it is associated with an XML variable or column of the appropriate type - an example is shown below.

CREATE TABLE SalesOrders

(OrderID integer PRIMARY KEY ,

OrderDate datetime,

CustomerID integer

OrderNotes xml(ProductSchema))

Typed XML is checked for compliance with the schema associated with a variable or column of the corresponding type when writing or updating data - this feature allows, for example, to guarantee the compliance of input data accepted standards or ensure compatibility with different types of documents.
The xml data type also supports a number of methods that can be used to query or manipulate XML data. For example, you can use the query method to retrieve data, as shown in the following example:

Declare @x xml

Set @x=

"

Kim Abercrombie

Margaret Smith

"

SELECT @x.query(

"

For $invoice in /Invoices/Invoice

Return $invoice/Customer

")

The above query uses XQuery syntax to look up all Invoice elements found in a given document and return an XML document that contains a Customer element for each Invoice element - an example of the resulting document is shown below.

< CustomerList >

< Customer >Kim Abercrombie

< Customer >Margaret Smith

Another new feature related to XML support introduced in SQL Server 2005 is support for XML indexes. It is possible to create primary and secondary XML indexes for xml type columns, which improves the performance of queries on XML data. The primary XML index is a compressed representation of all the branches of an XML document that the query processor uses to quickly find branches in the document. Once the primary XML index has been created, you can create a secondary XML index that can help improve performance for a number of specific queries. The following example shows how to create a primary XML index and a secondary XML index of type PATH, which will improve performance when running XPath_ queries on a given XML document.

CREATE PRIMARY XML INDEX idx_XML-Notes

ON SalesOrders (Notes)

CREATE XML INDEX idx_XML-Path_Notes

ON SalesOrders (Notes)

USING XML INDEX idx_XML-Notes

FOR PATH


The functionality provided in SQL Server 2000 and 2005 has been enhanced in SQL Server 2008. Key enhancements to XML support in SQL Server 2008 include improved schema validation capabilities, expanded XQuery support, and enhanced functionality for XML insertion. data using DML (Data Manipulation Language).

XSD extensions
Schema validation ensures that the XML document stored in SQL Server conforms to a specific standard and schema-level business rules. The schema level defines the elements and attributes allowed in an XML document, ensuring that the XML document contains the required data within a predefined structure.
SQL Server 2005 introduced support for validating XML data against XSD_schema collections. The approach is that you create a schema collection that contains schemas with rules for XML data using the CREATE XML SCHEMA COLLECTION command, and then reference the collection name when specifying a column or xml type variable that must conform to the rules described at the level scheme.
SQL Server validates the data entered or updated against the specified schema collection. SQL Server 2005 implements a subset of the full XML Schema specification and supports key XML input validation scenarios.
SQL Server 2008 expands support for XSD schemas by introducing additional features, which include support for any-level validation (so-called lax validation), full support for dateTime, time and date-level validation, including saving time zone information, and improved support for union and list types.
Template-level validation support is implemented at the level of the any, anyAttribute, and anyType constructs. For example, the following diagram:

< xs:complexType name = "Order" mixed = "true" >

< xs:sequence >

< xs:element name = "CustomerName" />

< xs:element name = "OrderTotal" />

< xs:any namespace = "##other" processContents = "skip" minOccurs = "0" maxOccurs = "unbounded" />

specifies an XML element named Order that must contain subelements named CustomerName and OrderTotal. In addition, an element can contain an unlimited number of other elements belonging to namespaces other than the one in which the Order type is defined.
The following XML document contains an instance of the Order element that matches the schema description. Note that the document also contains a shp:Delivery element, which is not described in the schema.

< Order >

< CustomerName >Graeme Malcolm

< OrderTotal >299.99

< shp:Delivery >Express


Pattern matching depends on the processContents attribute for the schema section in which the patterns are described. In SQL Server 2005, schemas can contain processContents attribute values ​​of skip and strict for any and anyAttribute declarations. In the previous example, the processContents attribute for the template was set to skip - so the contents of this element not checked. Even if the shp:Delivery element is described in the schema, it will not be checked until the processContents attribute is set to strict in the template description for the Order element.

SQL Server 2008 added a third possible value for the processContents attribute, lax, which allows you to specify that all elements described in the schema should be included in the validation, and elements not contained in the schema can be ignored. Thus, if in the previous example you set the processContents attribute for the template to lax, and added a description of the shp:Delivery element to the schema, this element will be included in the check. But since the shp:Delivery element is not described in the schema, it will not be included in the check. In addition, the XML Schema specification specifies that the anyType attribute is automatically subject to validation according to the rules described above. In SQL Server 2005, lax checking was not supported - by default, checking was performed at the strict level.
To specify data that describes a date and time, XML schemas use the dateTime data type. The date and time are specified in the following format: 2007 12 01T21:11:20:000Z, which represents December 1, 2007, 11:20 GMT - UTC (000Z). Other time zones in the following format: 000+3:00, for example, describes Moscow time. The XML Schema specification specifies the time zone component of the dateTime, date, and time data types as optional. However, SQL Server 2005 required the time zone to be specified when specifying date_Time, date, and time data types.
In addition, in SQL Server 2005, time zone data was not saved, but was converted to UTC - for example, the value 2007_12_25T06:00:00:000_8:00 became 2007_12_25T14:00:00:000Z. In SQL Server 2008, these restrictions are removed - when setting a date and time, you don't have to specify a time zone, but if you specify one, the data is saved correctly.
Developers can use XML schemas to define data types for XML data so that the data can contain a set of values ​​that can be assigned to elements and attributes. For example, you can define a sizeListType that limits the list of possible values ​​assigned to the AvaliableSizes element to S, M, and L. SQL Server 2005 supports schemas that contain simple definitions types and corresponding restrictions. For example, you can use the list type to specify possible sizes, as shown in the following example:

< xs:simpleType name = "sizeListType" >

< xs:list >

< xs:simpleType >

< xs:restriction base = "xs:string" >

< xs:enumeration value = "S" />

< xs:enumeration value = "M" />

< xs:enumeration value = "L" />


This schema description allows you to create an element that contains all possible sizes as a list of values ​​separated by spaces - this is shown in the following example:

< AvailableSizes >S M L

Microsoft Excel – handy tool for organizing and structuring a wide variety of data. It allows you to process information different methods, edit data sets.

Let's consider the possibilities of using it to generate and process web application files. On specific example Let's learn the basics of working with XML in Excel.

How to create an XML file from Excel

XML is a file standard for transmitting data on the Web. Excel supports its export and import.

Let's look at creating an XML file using the example of a production calendar.

  1. Let's make a table from which you need to create an XML file in Excel and fill it with data.
  2. Let's create and insert an XML map with the required document structure.
  3. Export table data to XML format.

We save the file as XML.

Other ways to get XML data (schema):

  1. Download from a database, specialized business application. Schemes can be provided by commercial sites and services. Simple options are in the public domain.
  2. Use ready-made samples to test XML maps. The samples contain the main elements, XML structure. Copy and paste into Notepad and save with the desired extension.


How to save an Excel file in XML format

One of the options:

  1. Click the Office button. Select “Save as” - “Other formats”.
  2. We assign a name. Select the save location and file type – XML.

More options:

  1. Download XLC to XML converter. Or find a service that allows you to export the file online.
  2. Download the add-on from the official Microsoft website XML Tools Add-in. It is freely available.
  3. Opening a new book. Office button – “Open”.

How to open an XML file in Excel

Click OK. You can work with the resulting table as with any Excel file.

How to Convert XML File to Excel

We edit the created table and save it in Excel format.

How to collect data from XML files in Excel

The principle of collecting information from multiple XML files is the same as the principle of transformation. When we import data into Excel, the XML map. Other data can be transferred to the same schema.

Each new file will be linked to an existing map. Each element in the table structure corresponds to an element in the map. Only one data binding is allowed.

To configure linking options, open the Map Properties tool from the Developer menu.


Possibilities:

  1. Each new file will be scanned Excel program for compliance installed card(if we check the box next to this item).
  2. Data may be updated. Or new information will be added to the existing table (makes sense if you need to collect data from similar files).

This is all manual methods import and export of files.

Data structuring is a useful thing, and will come in handy at the most unexpected moment. For example, if you structure your immediate future, you can see the size of your salary. And you can decide how efficiently you will work this entire month.

Of course, it’s science fiction, but a special language was invented for structuring programming ( xml). And to edit data presented in this format, specialized xml editors have been created:

What is XML

Many people know that the basis of any web resource is HTML. Using hypertext language, you can very easily and conveniently define the hierarchy of all elements on a web page. Partially, it allows us to solve the problem of styling. This entire hierarchy is created ( structured) using built-in tags and their attributes, the set of which, although not small, is still limited.

Therefore, HTML allows you to solve only one single problem of structuring on the Internet: arranging and creating elements of a web page for its subsequent markup ( design creation). But how to structure the rest of the data transferred in countless quantities between resources in world wide web? How to organize their selection, search and filtering without restrictions?

All these "shortcomings" can be corrected using XML. Before starting our review of xml editors, let's take a closer look at what this language is and what its scope is in the virtual space:


The abbreviation XML translated from English means “ extensible markup language» ( Xtensible Markup Language). It is standardized and recommended for use by the W3C. This means that its use is the most optimal and “problem-free” way to create a web document.

In addition to describing documents, XML also partially affects the operation of special system programs. Preprocessors of this language are designed to translate data from machine code into a user-friendly form. Such preprocessors are the basis of all xml file editors.

The extensible markup language has the following positive aspects:

  • Is the ideal remedy to describe the structure and markup of any web document;
  • In XML there is no limited set of elements with which structuring is carried out. Instead, the user himself sets the hierarchy and names of all elements, relying only on the rules of language description;
  • XML has a simple, understandable, and most importantly extensible syntax;
  • The language is built on the basic Unicode encodings;
  • It is widely used not only to describe ordinary web pages, but also easily connects to the code of most programming languages. Including those used in web programming ( PHP, ASP. NET and others ).

XML syntax

Every document written in XML consists of entities. An entity is the smallest unit (element). Each of the entities contains symbols.

They are divided into:

  • Markup symbols - tags, comments (< тег>, );
  • Literal characters - these are the main content contained between tags.

The logical structure of the language consists of hierarchically nested elements. The topmost one is called the root. Each element includes an opening and closing tag. And it should close in the root element in which it was opened:

Hello, world!

In addition to the root element, the XML document consists of a prologue. It is located at the very beginning of the code. The prologue may include:

  • Ads;
  • Processing instructions;
  • Comments.

The main components of XML are shown more clearly in the following screenshot of a document created in a simple xml editor:


You can learn more about the language syntax using the technical documentation for XML.

Overview of editors for XML

  • Microsoft Visual Studio is a powerful development environment that combines a large number of tools and tools for writing program code. It also includes a sophisticated XML editor. Among other things, it supports the creation and visualization of XML schemas. Unfortunately, PHP support in Visual Studio is weak. This environment is more “tailored” for creating web applications using ASP.NET:


  • Adobe Dreamweaver is another powerful development environment. Its entire toolkit is completely aimed at creating websites. The syntax of several programming languages ​​is supported. Dreamweaver also includes a built-in xml table editor:


  • XMLSpy XML Editor is a powerful tool for working with various formats XML data. Not only simple editing of data, diagrams, syntax highlighting is supported, but also graphical visualization of hierarchical relationships between elements:


The visual xml editor is available in two editions costing 399 and 799 euros. The most expensive option includes syntax support for several programming languages ​​and a debugger. The cheaper option is fully compatible with the most popular development environments ( Visual Studio, Eclipse), and can act as their full-fledged add-on:


  • XML Notepad - free editor xml. It has built-in syntax highlighting and a validation tool. As well as support for constructing circuits. The application has a simple and intuitive interface:


Review of online services

  • XML Schema Generator - a service that allows you to create a schema (XSD) from a regular XML document. The service interface is simple and designed in black and white. In addition, we are pleased with the complete absence of advertising;
  • xmlvalidation.com – this resource allows you to fully validate the content of an XML document copied into a special field or uploaded as a file:


  • XSL Transformation – serves to transform regular XML code using specified XSLT style templates. The service also includes several dozen more tools useful for webmasters, including an online XML validator.

As you can see from the review, to work with an extensible markup language, it is best to use a regular xml editor installed own computer or laptop. The set of tools of specialized online services is not so wide, and only allows you to check the XML document code for validity. Or convert its contents into a schema.

XML for PHP Developers

Part 1. Working with XML in PHP in 15 minutes

PHP5 greatly simplifies working with XML in PHP

Content Series:

The importance of XML in the modern application development environment cannot be overstated. For those who have never worked with XML in PHP or have not yet migrated to PHP5, this quick tutorial will help you get started with the new XML-related functionality of PHP5 and appreciate how easy working with XML can be. This first of three articles introduces the API and demonstrates how SimpleXML, when combined with the DOM, is an ideal tool for developers dealing with simple, predictable, and relatively compact XML documents. It is precisely these documents that, for example, Ajax applications operate when transmitting information about filling out a form; API responses from Web services such as weather.com are compiled into the same documents.

XML Basics

A brief introduction to the basics of XML will allow the reader to understand the importance of this language for the PHP developer and learn how to create simple XML documents.

About XML

Extensible Markup Language (XML) can be called both a markup language and a format for storing text data. It is a subset of the Standard Generalized Markup Language (SGML); it provides text tools to describe tree structures and their application to information. XML serves as the basis for a number of languages ​​and formats, such as Really Simple Syndication (RSS), Mozilla XML User Interface Language (XUL), Macromedia Maximum eXperience Markup Language (MXML), Microsoft eXtensible Application Markup Language (XAML) and open source language Java XML UI Markup Language (XAMJ).

XML structure

The basic unit of data in XML is the element. Elements are highlighted with a start tag such as , and an end tag such as. Each start tag must have a corresponding end tag. If any start tag is missing an end tag, the XML document is not formatted correctly and the parser will not be able to parse it properly. Tag names usually reflect the type of element. You might expect the book element to contain the title of the book, such as "The Great American Novel" (see Listing 1). The text contained between tags, including spaces, is called character data.

Listing 1. Example XML document
Great American Novel Cliff great guy Pretty Woman rare beauty A faithful dog likes to sleep Cliff meets a Pretty Woman. The Loyal Dog sleeps but wakes up to bark at the postman. 4 9

XML element and attribute names can consist of uppercase (A-Z) and lowercase (a-z) letters, numbers (0-9), some special and non-English characters, and three punctuation characters: hyphen, underscore, and period. Other characters are not allowed in names.

XML is case sensitive. In the given example And describe two different elements. Both names are acceptable. However, describing two different elements by names And cannot be considered a reasonable solution due to the high probability of typos.

Every XML document contains one and only one root element. The root element is the only element in an XML document that does not have a parent element. In the example above, the root element is . Most XML documents contain parent and child elements. Element has one child element . At the element four child elements: , <characters>, <plot>AND <success>. At the element <characters>three children, each of which is an element <character>. Each element <character>two child elements each, <name>And <desc>.</p><p>In addition to nested elements, which create a parent-child relationship, XML elements can have attributes. These are name-value pairs appended to the element's start tag. Names are separated from values ​​by an equal sign, =. Values ​​are enclosed in single or <a href="https://gtavrl.ru/en/otkuda-otkryvayutsya-kavychki-kak-izmenit-vid-kavychek-s-pryamyh/">double quotes</a>. There is 1 element in the listing <success>has two attributes: "bestseller" and "bookclubs". XML developers practice <a href="https://gtavrl.ru/en/kak-uznat-na-chto-spros-v-gorode-kak-vybirat-hodovoi-tovar-dlya-svoei-nishi-v/">different approaches</a> to the use of attributes. Most of the information contained in an attribute can be placed in a child element. Some developers insist that attribute information consist not of data but of metadata, that is, information about the data. The data itself must be contained in the elements. In reality, the decision about whether to use attributes depends on the nature of the data and how it is extracted from the XML.</p><h3>Advantages of XML</h3><p>One of the advantages of XML is its relative simplicity. An XML document can be compiled in a simple way <a href="https://gtavrl.ru/en/kak-otkryt-tekstovyi-redaktor-kak-otkryt-tekstovye-redaktory/">text editor</a> or word processor without resorting to special tools or software. Basic XML syntax consists of nested elements, some of which have attributes and content. Typically an element begins with an opening tag<тег>and ends with the appropriate closing tag</тег >. XML is case sensitive and does not ignore spaces or tabs. It is very similar to HTML, but unlike HTML, it allows tags to be named for <a href="https://gtavrl.ru/en/total-kommander-opisanie-programmy-total-kommander-luchshii-failovyi/">better description</a> your data. The advantages of XML include self-documentation, a human- and computer-readable format, and Unicode support, which allows you to create documents in <a href="https://gtavrl.ru/en/smail-pokazat-yazyk-chto-oznachayut-raznye-smaily-pri-obshchenii-s-inostrancami/">different languages</a>, And <a href="https://gtavrl.ru/en/mozhno-li-zarabotat-na-instagramme-trebovaniya-k-uspehu-samye-prostye-sposoby/">simple requirements</a> to syntax and parsing. Unfortunately, UTF-8 support in PHP5 is fraught with problems; this is one of the shortcomings that led to the development of PHP6.</p><h3>Disadvantages of XML</h3><p>XML is verbose and redundant, resulting in large documents that take up a lot of space. <a href="https://gtavrl.ru/en/chto-delat-esli-aifon-ne-obnovlyaetsya-oshibka-zagruzki-obnovleniya-vozvrashchenie/">disk space</a> And <a href="https://gtavrl.ru/en/vozmozhnye-varianty-samostoyatelnogo-resheniya-oshibki-dns-server-ne-otvechaet/">network resources</a>. It's supposed to be human readable, but it's hard to imagine a person trying to read an XML file with 7 million nodes. Protozoa <a href="https://gtavrl.ru/en/brauzer-ponyatie-vidy-naznachenie-tipy-sintaksicheskih-analizatorov-kakoi/">parsers</a> functionally unable to support a wide range of data types; for this reason, rare or unusual data, of which there are many, become a serious source of difficulty.</p><h3>Well-formed XML</h3><p>An XML document is considered to be well-formed if it follows the rules of XML syntax. An incorrectly constructed format is not, in a technical sense, an XML document. For example, an HTML tag like <br>, in XML is not acceptable; corresponding <a href="https://gtavrl.ru/en/samye-populyarnye-tegi-dlya-vk-pravilnyi-podbor-heshtegov-v/">correct tag</a> looks like <br />. The root element can be thought of as an endless filing cabinet. You only have one closet, but there are almost no restrictions on the type and quantity of its contents. Your closet can accommodate an endless number of drawers and file folders.</p><h2>PHP Basics</h2><p>Most readers of this article are already working with PHP, but may be unfamiliar with its history and evolution.</p><h3>About PHP</h3><p>Hypertext Preprocessor (PHP) is a platform-independent scripting language used to create dynamic Web pages and server applications. It started out as Personal Home Page/Form Interpreter (PHP/FI) and became <a href="https://gtavrl.ru/en/chto-zhe-vybrat-ili-vmesto-vyvoda-sozdaem-novuyu-zhizn-novoe/">new life</a> in the hands of Suraski and Gutmans, who released PHP3 in June 1998. Their company, Zend Technologies, still manages PHP development.</p><p>In July 2004, PHP5 was released with Zend Engine II and many new features such as:</p><ul><li>Object-oriented programming support</li><li>Improved MySQL support</li><li>Improved XML support</li> </ul><h3>PHP5 and XML</h3><p>XML support has been present in PHP since the earliest versions, but it has been greatly improved in PHP5. XML support in PHP4 was limited, including only a default SAX-based parser, and DOM support was not W3C compliant. In PHP5, the PHP XML developers reinvented the wheel by ensuring compliance with generally accepted standards.</p><h3>New in XML support in PHP5</h3><p>PHP5 contains completely rewritten and new extensions, including the SAX parser, DOM, SimpleXML, XMLReader, XMLWriter and XSLT processor. Now all these extensions are based on libxml2.</p><p>Along with improved SAX support compared to PHP4, PHP5 implements DOM support in accordance with the W3C standard, as well as the SimpleXML extension. By default, both SAX, DOM, and SimpleXML are enabled. Those familiar with the DOM from other languages ​​will find it easier to implement similar functionality in PHP</p><h2>Reading, Processing and Writing XML in PHP5</h2><p>SimpleXML, optionally combined with the DOM, is the ideal choice for reading, processing, and composing simple, predictable, and relatively compact documents in PHP5.</p><h3>Brief overview of preferred APIs</h3><p>Of the many APIs present in PHP5, DOM is the most familiar, and SimpleXML is the easiest to program with. In typical situations, such as the ones we're looking at here, they are most effective.</p><h3>DOM extension</h3><p>The Document Object Model (DOM) is a W3C standard set of objects for <a href="https://gtavrl.ru/en/kak-vstavit-izobrazhenie-v-html-dokument-vstavka-izobrazhenii-v-html-dokument/">HTML documents</a> and XML, a standard model for combining these objects and a standard interface for accessing and manipulating them. Many vendors support the DOM as an interface to their custom data structures and APIs, making the DOM familiar to many developers. The DOM is easy to learn and use because its in-memory structure resembles the original XML document. To convey information to the application, the DOM creates a tree of objects that exactly replicates the tree of elements <a href="https://gtavrl.ru/en/tekstovyi-redaktor-xml-otkryvaem-fail-xml-dlya-redaktirovaniya/">XML file</a>, so that each XML element serves as a node in this tree. DOM is a parser based on a tree structure. Since the DOM builds a tree of the entire document, it consumes a lot of memory and CPU time. Therefore, parsing very large documents via the DOM is impractical due to performance issues. In the context of this article, the DOM extension is used primarily for its ability to import SimpleXML format and output XML in DOM format, or conversely, for use as string data or an XML file.</p><h3>SimpleXML</h3><p>The SimpleXML extension is the tool of choice for parsing XML. It requires PHP5 to work. It interacts with the DOM when composing XML files and has built-in XPath support. SimpleXML works best with simple record-type data, such as XML transmitted as a document or string from another part of the same application. Unless the XML document is too complex, too deep, or contains mixed content, SimpleXML is easier to code than DOM, as the name suggests. In addition, it is more reliable when working with a known document structure.</p><h3>Simple examples</h3><p>Below are <a href="https://gtavrl.ru/en/kak-sostavit-semanticheskoe-yadro-stati-prostoi-primer-sostavleniya/">simple examples</a> working with DOM and SimpleXML on compact XML files.</p><h2>DOM in action</h2><p>The DOM implemented in PHP5 is the same W3C DOM specification that you deal with in the browser and interact with through JavaScript. The same methods are used, so the coding methods will seem familiar to you. Listing 2 illustrates the use of the DOM to create an XML string and an XML document formatted for readability.</p><h5>Listing 2. Using the DOM</h5><span> <?php //Создает XML-строку и XML-документ при помощи DOM $dom = new DomDocument("1.0"); //добавление корня - <books>$books = $dom->appendChild($dom->createElement("books")); //adding an element <book>V <books>$book = $books->appendChild($dom->createElement("book")); // adding an element <title>V <book>$title = $book->appendChild($dom->createElement("title")); // adding a text node element <title>V <title>$title->appendChild($dom->createTextNode("Great American Novel")); //xml generation $dom->formatOutput = true; // setting the formatOutput attribute // domDocument to true // save XML as string or file $test1 = $dom->saveXML(); // passing a string to test1 $dom->save("test1.xml"); // saving the file?></span><p>This produces the output file shown in Listing 3.</p><h5>Listing 3. Output file</h5><span> <?xml version="1.0"?> <books> <book> <title>Great American Novel

Listing 4 imports a SimpleXMLElement object into a DOMElement object, illustrating how the DOM and SimpleXML interact.

Listing 4. Interop Part 1 - DOM imports SimpleXML
". "Great American Novel"); if ($sxe === false) ( echo "Error while parsing the document"; exit; ) $dom_sxe = dom_import_simplexml($sxe); if (!$dom_sxe) ( echo "Error while converting XML"; exit ; ) $dom = new DOMDocument("1.0"); $dom_sxe = $dom->importNode($dom_sxe, true); $dom_sxe = $dom->appendChild($dom_sxe); echo $dom->save("test2 .xml"); ?>

The function in Listing 5 takes a DOM document node and turns it into a SimpleXML node. This new object can then be used as a native SimpleXML element. In case of any error, FALSE is returned.

Listing 5. Interop Part 2 - SimpleXML Imports the DOM
loadXML(" Great American Novel"); if (!$dom) ( echo "Error while parsing the document"; exit; ) $s = simplexml_import_dom($dom); echo $s->book->title; // Great American Novel ?>

SimpleXML in action

The SimpleXML extension is the tool of choice for parsing XML. It interacts with the DOM when composing XML files and has built-in XPath support. SimpleXML is easier to code than DOM, as its name suggests.

For those unfamiliar with PHP, Listing 6 formats the test XML file as an include file for readability purposes.

Listing 6: In the following examples, the test XML file is formatted as a PHP include file named example.php.
Great American Novel Cliff really great guy Lovely Woman matchless beauty Loyal Dog sleepy 4 9 XML; ?>

In a hypothetical Ajax application, for example, you might want to extract a zip code from an XML document and access a database. Listing 7 extracts the element from the XML file of the previous example

Listing 7. Retrieving a node - how easy is it to get it?
book->plot; // "Cliff meets Lovely Woman...." ?>

On the other hand, you may need to extract a multi-line address. When a single parent element has multiple instances of a child element, the normal iteration technique is used. This functionality is shown in Listing 8.

Listing 8. Retrieving multiple instances of an element
node, echo a separate . */ foreach ($xml->book as $book) ( echo $book->plot, "
"; } ?>

In addition to reading element names and their values, SimpleXML can also access element attributes. Listing 9 accesses the element's attributes; this is done in exactly the same way as accessing the elements of an array.

Listing 9. Demonstration of SimpleXML accessing element attributes
Great American Novel Cliff really great guy Lovely Woman matchless beauty Loyal Dog sleepy Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark at mailman. 4 9 XML; ?>nodes of the first book. * Output the success indications, too. */ foreach ($xml->book->success as $success) ( switch((string) $success["type"]) ( // Get attributes as element indices case "bestseller": echo $success, " months on bestseller list"; break; case "bookclubs": echo $success, "bookclub listings"; break; ) ) ?>

To compare an element or attribute to a string, or to pass it to a function that requires a string, you must convert it to a string using the (string) operator. Otherwise, by default, PHP treats the element as an object (Listing 10).

Listing 10. Convert to a string or lose
book->title == "Great American Novel") { print "My favorite book."; } htmlentities((string) $xml->book->title); ?> !}

Data in SimpleXML does not have to be immutable. Listing 11 outputs a new XML document that is an exact copy of the original, except that the new one changes the name Cliff to Big Cliff.

Listing 11. Modifying a text node using SimpleXML
Great American Novel Cliff really great guy Lovely Woman matchless beauty Loyal Dog sleepy Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark at mailman. 4 9 XML; ?>book->characters->character->name = "Big Cliff"; echo $xml->asXML(); ?>

Since PHP 5.1.3, SimpleXML has been enhanced with the ability to easily add child elements and attributes. Listing 12 outputs an XML document based on the original one, but with a new character and description added.

Listing 12. Adding child and text nodes using SimpleXML
Great American Novel Cliff really great guy Lovely Woman matchless beauty Loyal Dog sleepy Yellow Cat aloof Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark at mailman. 4 9 XML; ?>book->characters->addChild("character"); $character->addChild("name", "Yellow Cat"); $character->addChild("desc", "aloof"); $success = $xml->book->addChild("success", "2"); $success->addAttribute("type", "reprints"); echo $xml->asXML(); ?>

Conclusion

This first article in a three-part series introduces the API and demonstrates that SimpleXML, when combined with the DOM when needed, is an ideal tool for developers dealing with simple, predictable, and relatively compact XML documents. Version PHP5 has significantly expanded the capabilities of programmers in terms of working with XML in PHP. In part two we will look at more complex methods XML parsing.







2024 gtavrl.ru.