2 2 2 Advanced XML

Embed Size (px)

Citation preview

  • 7/29/2019 2 2 2 Advanced XML

    1/31

    1

    Dr Alexiei Dingli

    XML Technologies

    XML Advanced

  • 7/29/2019 2 2 2 Advanced XML

    2/31

  • 7/29/2019 2 2 2 Advanced XML

    3/31

    3

    Since XML element names can be defined

    by anyone, this often results in a conflict

    Such as

    How would you add XML related to furniture(tables and chairs) to an HTML document?

    Conflicts !!!

  • 7/29/2019 2 2 2 Advanced XML

    4/31

  • 7/29/2019 2 2 2 Advanced XML

    5/31

    5

    If these XML fragments were added together,

    there would be a name conflict

    Both contain a element, but theelements have different content and meaning

    An XML parser will not know how to handlethese differences

    Even more conflicts !!!

  • 7/29/2019 2 2 2 Advanced XML

    6/31

    6

    Name conflicts in XML can easily be avoided using a name prefix This XML carries information about an HTML table, and a piece of

    furniture:

    Apples

    Bananas

    Coffee Table

    8

    10

    In the example above, there will be no conflict because the two

    elements have different names.

    Solving conflicts ...

  • 7/29/2019 2 2 2 Advanced XML

    7/31

    7

    The namespace is defined by the xmlnsattribute in the start tag of an element

    The namespace declaration has the

    following syntax

    xmlns:prefix="URI"

    XML Namespaces

  • 7/29/2019 2 2 2 Advanced XML

    8/31

    8

    Apples

    Bananas

    African Coffee Table

    80

    120

    Namespaces Ex 1

  • 7/29/2019 2 2 2 Advanced XML

    9/31

    9

    Apples

    Bananas

    African Coffee Table

    80

    120

    Namespaces Ex 2

  • 7/29/2019 2 2 2 Advanced XML

    10/31

    10

    The xmlns attribute in the tag gave the h: and f: prefixes a

    qualified namespace

    When a namespace is defined for an element, all child elements with

    the same prefix are associated with the same namespace

    Namespaces can be declared in the elements where they are used

    or in the XML root element

    The namespace URI is not used by the parser to look up information

    The purpose is to give the namespace a unique name, however,

    often companies use the namespace as a pointer to a web page

    containing namespace information

    More on Namespaces

  • 7/29/2019 2 2 2 Advanced XML

    11/31

  • 7/29/2019 2 2 2 Advanced XML

    12/31

  • 7/29/2019 2 2 2 Advanced XML

    13/31

  • 7/29/2019 2 2 2 Advanced XML

    14/31

  • 7/29/2019 2 2 2 Advanced XML

    15/31

  • 7/29/2019 2 2 2 Advanced XML

    16/31

  • 7/29/2019 2 2 2 Advanced XML

    17/31

    17

    XML documents can contain non ASCII

    characters, like French .

    To avoid errors, specify the XML encoding

    Two encoding errors: An invalid character was found in text content.

    Switch from current encoding to specified encoding notsupported.

    XML Encoding

  • 7/29/2019 2 2 2 Advanced XML

    18/31

    18

    Different encodings ...

  • 7/29/2019 2 2 2 Advanced XML

    19/31

    19

    DOM (Document Object Model) defines a standard way for

    accessing and manipulating documents

    Consider XML documents as a tree-structure

    All elements can be accessed through the DOM tree

    Their content (text and attributes) can be modified or deleted, and

    new elements can be created

    The elements, their text, and their attributes are all known as nodes

    DOM

  • 7/29/2019 2 2 2 Advanced XML

    20/31

  • 7/29/2019 2 2 2 Advanced XML

    21/31

  • 7/29/2019 2 2 2 Advanced XML

    22/31

    22

    x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];

    Retrieves the text value of the first element

    txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");

    retrieves the text value of the "lang" attribute of the first element

    Advanced DOM examples

  • 7/29/2019 2 2 2 Advanced XML

    23/31

  • 7/29/2019 2 2 2 Advanced XML

    24/31

  • 7/29/2019 2 2 2 Advanced XML

    25/31

  • 7/29/2019 2 2 2 Advanced XML

    26/31

    26

    The CD Catalogue (XML)

    The CD Catalogue (Code)

    The CD Catalogue (HTML)

    XML to HTML

    http://localhost/var/www/apps/conversion/tmp/scratch_4/examples/cd_catalog.xmlhttp://localhost/var/www/apps/conversion/tmp/scratch_4/examples/cd_catalogue.txthttp://localhost/var/www/apps/conversion/tmp/scratch_4/examples/cd_catalogue.htmlhttp://localhost/var/www/apps/conversion/tmp/scratch_4/examples/cd_catalogue.htmlhttp://localhost/var/www/apps/conversion/tmp/scratch_4/examples/cd_catalogue.txthttp://localhost/var/www/apps/conversion/tmp/scratch_4/examples/cd_catalog.xml
  • 7/29/2019 2 2 2 Advanced XML

    27/31

    27

    We check the browser, and load the XML using the correct parser

    We create an HTML table with

    We use getElementsByTagName() to get all XML CD nodes

    For each CD node, we display data from ARTIST and TITLE as table

    data.

    We end the table with

    XML to HTML explanation

  • 7/29/2019 2 2 2 Advanced XML

    28/31

  • 7/29/2019 2 2 2 Advanced XML

    29/31

  • 7/29/2019 2 2 2 Advanced XML

    30/31

    30

    xmlhttp.open("GET",url,true);

    Our examples use "true" in the third parameter of open()

    This parameter specifies whether the request should be handled

    asynchronously True means that the script continues to run after the send() method,

    without waiting for a response from the server

    The onreadystatechange event complicates the code. But it is the

    safest way if you want to prevent the code from stopping if you don't

    get a response from the server. By setting the parameter to "false", your can avoid the extra

    onreadystatechange code. Use this if it's not important to execute

    the rest of the code if the request fails.

    XMLHttpRequest Async

  • 7/29/2019 2 2 2 Advanced XML

    31/31