How XML works

If you look at an XML document, you may think that it's similar to HTML documents, because both of them have tags. But no, HTML documents follow a certain set of rules defined by the language. In the case of XML, the rules are defined by the document itself, as in the following example:

<?xml version="1.0" encoding="UTF-8"?> 
<animals> 
<fish> 
<name>Discus</name> 
<location>Brasil/location> 
<danger_extintion="1">Shot the web</danger_extintion> 
</fish> 
</animals> 

You can see that the document itself defined each tag and how to use it, along with the values, and so on. This is the reason why applications such as MS Office use XML to manage their documents in the background.

For example, let's look at a Word document:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> 
   <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" 
                 Target="word/document.xml"/> 
</Relationships> 
 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> 
</Relationships> 
 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> 
   <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> 
   <Default Extension="xml" ContentType="application/xml"/> 
   <Override PartName="/word/document.xml" 
             ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> 
</Types> 
 
<w:document> 
   <w:body> 
       <w:p w:rsidR="005F670F" w:rsidRDefault="005F79F5"> 
           <w:r><w:t>Test</w:t></w:r> 
       </w:p> 
       <w:sectPr w:rsidR="005F670F"> 
           <w:pgSz w:w="12240" w:h="15840"/> 
           <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" 
                    w:gutter="0"/> 
           <w:cols w:space="720"/> 
           <w:docGrid w:linePitch="360"/> 
       </w:sectPr> 
   </w:body> 
</w:document> 
 
<w:p w:rsidR="0081206C" w:rsidRDefault="00E10CAE"> 
<w:r> <w:t xml:space="preserve">This is our example first paragraph. It's default is left aligned, and now I'd like to introduce</w:t> </w:r> 
    <w:r> <w:rPr> 
    <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> 
    <w:color w:val="000000"/> 
    </w:rPr> <w:t>some bold</w:t> 
    </w:r> 
    <w:r> <w:rPr> 
    <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> 
    <w:b/> <w:color w:val="000000"/> 
    </w:rPr> <w:t xml:space="preserve"> text</w:t> 
    </w:r> 
    <w:r>  <w:rPr> <w:rFonts w:ascii="Arial" 
w:hAnsi="Arial" w:cs="Arial"/> <w:color w:val="000000"/> </w:rPr> 
    <w:t xml:space="preserve">, </w:t> 
    </w:r> 
    <w:proofErr w:type="gramStart"/> 
    <w:r> <w:t xml:space="preserve">and also change the</w:t> </w:r> 
    <w:r w:rsidRPr="00E10CAE">  <w:rPr><w:rFonts w:ascii="Impact" w:hAnsi="Impact"/> 
    </w:rPr>  <w:t>font style</w:t> </w:r> 
    <w:r> 
    <w:rPr>  <w:rFonts w:ascii="Impact" w:hAnsi="Impact"/>  </w:rPr>  <w:t xml:space="preserve"> </w:t> 
    </w:r> 
    <w:r> <w:t>to 'Impact'.</w:t></w:r> 
    </w:p> 
    <w:p w:rsidR="00E10CAE" w:rsidRDefault="00E10CAE"> <w:r> <w:t>This is new paragraph.</w:t> </w:r></w:p> 
    <w:p w:rsidR="00E10CAE" w:rsidRPr="00E10CAE" w:rsidRDefault="00E10CAE"> 
    <w:r> <w:t>This is one more paragraph, a bit longer.</w:t> </w:r> 
    </w:p> 
... 

Here, you can see how Microsoft is defining each element in the document, such as the type, characteristics, rules, spaces, and the information in the document itself.

The other important feature of XML is that it can be used to send and receive information. This means I can send an HTTP request, which includes an XML document with information, that will be parsed in a server, and then, after it has been processed, it receives the result in an XML document.

This derives in the use of XML for web services and APIs, which are a different model for applications. When you use web services, you create task or actions, and expose these actions—this means you provide access to use them—by sending the information in a specific format. This format is usually an XML document, but nowadays JSON is also another popular format.

If an application does not receive a request in a valid format, the process results in an error, but if the request is received as the application waits for it, the result will be processed. These formats are defined in a Document Type Definition (DTD) document. And if you can change this document, you also can force the application to understand the documents in a different way.

The DTDs can be stored in the server or also can be included in the document, as shown in this example:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE Animal [ 
<!ELEMENT Fish (Name, Species, Origin, Data)> 
<!ELEMENT Name (#PCDATA)> 
<!ELEMENT Species (#PCDATA)> 
<!ELEMENT Origin(#PCDATA)> 
<!ATTLIST Danger_Extiintion optional CDATA "0"> 
<!ELEMENT Data ANY> 
<!ENTITY url SYSTEM "website.txt"> 
]> 
<animal> 
XML External Entity Vulnerability 88 
<fish> 
<name>Discus</name> 

Using these definitions, you can add whatever you want to and it would be parsed. Also, you can add references to resources.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.124.65