XML and JSON Open Message Formats

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which characteristic distinguishes XML from HTML?

  • HTML is about displaying information, while XML focuses on describing information. (correct)
  • XML is an inconsistent standard, whereas HTML requires all tags to be closed.
  • XML defines the valid tags and structure for web pages, while HTML is used to mark up text.
  • HTML is a meta-language used to define other languages, while XML itself has predefined tags.

Why is XML considered a cross-platform, software, and hardware-independent tool?

  • It can be used to display information on web pages.
  • It provides a standardized way to transmit information and data. (correct)
  • It requires specific software to run.
  • It is only compatible with specific operating systems.

In XML, what purpose do namespaces serve?

  • To display information in a standardized format.
  • To avoid naming conflicts when using the same element name for different things. (correct)
  • To act as a language used to define other languages.
  • To define the valid tags and structure for web pages.

Considering an XML document representing course information, which element would be considered the 'root' element?

<p><courselist> (B)</p> Signup and view all the answers

What does it mean for an XML file to be 'valid'?

<p>It must adhere to a defined XML Schema Definition (XSD) that dictates its structure and semantics. (B)</p> Signup and view all the answers

In the context of XML, what is the primary function of XSLT (extensible stylesheet language transformations)?

<p>To transform XML documents into other formats, like HTML. (D)</p> Signup and view all the answers

In XML, what does an XML Schema Definition (XSD) primarily define?

<p>The structure, data types, and constraints of elements and attributes in an XML document. (D)</p> Signup and view all the answers

When considering the choice between XML and binary formats for data transfer, what is an advantage of using binary formats?

<p>They result in smaller data sizes and reduced overhead. (A)</p> Signup and view all the answers

How does XML contribute to interoperability in data exchange?

<p>By being an open and standardized textual data format. (B)</p> Signup and view all the answers

In the context of data serialization, what is the primary goal?

<p>To convert an object into a single, transportable stream. (C)</p> Signup and view all the answers

What is the main advantage of using XML for data transmission, particularly in scenarios requiring self-describing messages?

<p>XML is self-describing and platform neutral. (A)</p> Signup and view all the answers

What is a primary distinction between XML and JSON concerning namespaces?

<p>XML natively supports namespaces. (D)</p> Signup and view all the answers

In terms of data types, how does JSON compare with XML?

<p>XML values are type-less strings. (C)</p> Signup and view all the answers

Which feature is notably lacking in JSON compared to XML?

<p>Support for namespaces. (D)</p> Signup and view all the answers

What does it imply when JSON is referred to as lightweight?

<p>Has less features. (D)</p> Signup and view all the answers

Which of the following best describes the syntax of a JSON object?

<p>Unordered sets of name/value pairs enclosed in curly braces {}. (C)</p> Signup and view all the answers

How are name/value pairs separated within a JSON object?

<p>Colon (:) (D)</p> Signup and view all the answers

What is the primary purpose of the curly braces {} in JSON syntax?

<p>Enclose objects. (D)</p> Signup and view all the answers

Which of the following is true regarding JSON arrays?

<p>JSON arrays are ordered collections of values. (D)</p> Signup and view all the answers

What is the proper way to define a string in JSON?

<p>Using double quotes. (C)</p> Signup and view all the answers

What value is used in JSON to represent 'nothing or no value'?

<p>null (C)</p> Signup and view all the answers

What could be a potential consideration when deciding to use JSON for configuration files, instead of YAML?

<p>JSON enforces stricter syntax. (C)</p> Signup and view all the answers

Which format is most suitable for transferring data to and from a server via asynchronous calls, without requiring a page refresh?

<p>JSON (B)</p> Signup and view all the answers

When is it appropriate to use JSON in web development?

<p>When transferring data to and from a server. (D)</p> Signup and view all the answers

What is the main concept of JSON in the context of data?

<p>Lightweight. (C)</p> Signup and view all the answers

Which of the following could be a disadvantage of using XML format over JSON format?

<p>Parsing can be a complicated process. (C)</p> Signup and view all the answers

What does the term well-formed mean in the context of XML documents?

<p>The document must adhere to general XML syntax rules, such as having matching start and end tags. (B)</p> Signup and view all the answers

What are the key factors that determine the use-case of binary data formats in network protocols?

<p>They are traditionally used for network protocols and data transfer. (A)</p> Signup and view all the answers

Which of the following technologies is used to format XML documents?

<p>XSLT (A)</p> Signup and view all the answers

What is the role of XML in defining a format for data transmission over the internet?

<p>It defines open and standardized textual data formats. (C)</p> Signup and view all the answers

Which statement accurately reflects a key distinction between XML and JSON?

<p>XML offers a richer set of features. (A)</p> Signup and view all the answers

During data transmission, what is the purpose of serialization?

<p>Convert into a transportable stream. (D)</p> Signup and view all the answers

When defining elements and attributes in XML, which of the following is true?

<p>Element and attribute names should be descriptive and not confusing. (A)</p> Signup and view all the answers

What must all XML values, including numbers, be enclosed in?

<p>Double quotes (D)</p> Signup and view all the answers

What is the purpose of the elementFormDefault="qualified" attribute in an XML schema?

<p>Elements used by an XML document which were declared in this schema must be namespace qualified. (C)</p> Signup and view all the answers

Consider this snippet:

<book xmlns="http://www.bookstuff.org/bookinfo">
<title>All About XML</title>
<author>Joe Developer</author>
<publisher xmlns="urn:publishers:publinfo">
<name>Microsoft Press</name>
</publisher>
</book>

Which elements belong to the http://www.bookstuff.org/bookinfo namespace?

<p>title and author (D)</p> Signup and view all the answers

What is the primary indicator that an XSD element uses data types and elements from the XMLSchema, requiring a prefix?

<p>xmlns:xs attribute (C)</p> Signup and view all the answers

For which use case is JSON particularly well-suited?

<p>Client-server communication in web applications. (C)</p> Signup and view all the answers

What syntax is necessary when creating a JSON document?

<p>It must be straightforward. (C)</p> Signup and view all the answers

Flashcards

What is XML?

Stands for eXtensible Markup Language. It is used to structure, store, and transport data.

What is Marked-up text?

Text that is structured with textual tags providing meaning to the contents.

XML vs. HTML

Unlike HTML that is about displaying information, XML focuses on describing it. It is also self describing and platform neutral

What is a Meta Language?

A language used to define other languages. XML is a meta language. XML itself has no tags

Signup and view all the flashcards

Why use XML?

A markup language for documents containing structured data that is human-readable. It provides a cross-platform tool for information transmission.

Signup and view all the flashcards

What is a root element?

The element that contains all other elements in an XML document.

Signup and view all the flashcards

What are XML elements (or tags)?

The basic components of an XML document, enclosed in angle brackets.

Signup and view all the flashcards

What are XML Attributes?

Provide extra information about elements and can have a default value.

Signup and view all the flashcards

What are benefits of XML?

A W3C standard that enables data representation across diverse environments, allowing a high degree of interoperability.

Signup and view all the flashcards

What is XML Schema?

Defines a set of elements, attributes, and rules that XML documents must follow to be considered valid.

Signup and view all the flashcards

What are XML Namespaces?

Used to avoid name conflicts when multiple XML documents use the same element names.

Signup and view all the flashcards

What is Default Namespace?

Declared without a prefix, applying to unqualified elements within its scope.

Signup and view all the flashcards

What is XSLT?

A language for transforming XML documents into other formats.

Signup and view all the flashcards

What is Binary data format?

Maps data concepts to the native entities of the computer system, often as bytes.

Signup and view all the flashcards

What is Text data format?

Maps data to character strings that require translation to be understood by computers.

Signup and view all the flashcards

What is Serialization?

Converting an object into a single, transportable stream.

Signup and view all the flashcards

What is Deserialization?

The process of recreating an object from a serialized string.

Signup and view all the flashcards

What is JSON?

Stands for JavaScript Object Notation, is lightweight text-based data-interchange format.

Signup and view all the flashcards

JSON Object Syntax

Unordered sets of name/value pairs. Begins with { and end with }.

Signup and view all the flashcards

When is JSON is Used?

Used for when transferring data to and from a server, and to perform asynchronous data calls without requiring a page refresh

Signup and view all the flashcards

Study Notes

Lecture 7 – Open Message Formats

  • Covers XML and JSON for network object transfer via serialization.
  • Focuses on XML and JSON since they are message formats widely used in RPC methods for data transfer.

What is XML?

  • XML is the eXtensible Markup Language.
  • It structures text by encasing it with textual tags, describing the meaning of text content.
  • The markup language defines valid tags and structure without dictating their use.

Use of ASCII vs XML

  • ASCII can store data for retrieval across platforms, however, the meaning is not clear. An example is "10, Nimal, 56"

  • XML is self-describing and platform-neutral, like:

    10 Nimal 56

XML vs. HTML

  • HTML defines valid tags and structure for web pages.
  • HTML has a Fairly inconsistent standard; tags don't always need closing (e.g., <p>).
  • XML can be used to mark up text
  • It's a Meta Language: a language used to define other languages.
  • XML itself has no tags: XML tags are not predefined, requiring you to 'invent' your own tags for a new language.
  • HTML is about displaying information, while XML is about describing information.

Why Use XML?

  • XML is a markup language for documents with structured data
  • It describes data in human-readable, structured form.
  • Structured information can contain both content, as well as information that defines the content.
  • It's a cross-platform, software, and hardware-independent tool for transmitting information/data.

Where To Use XML

  • XML finds its use in e-commerce (B2B), content management, web services, distributed computing, peer-to-peer (P2P), and the Semantic Web.
  • However, it can need a lot of space
  • 3-20 times larger compared to binary format.
  • XML plays a role for the future of the Web, similar to HTML.
  • It's a common tool for data manipulation and data transmission.

XML as a Universal Language

  • XML is a "use everywhere" data specification.

XML Document Sample

  • XML doc:
courselist
   course
      lecturer
         first name
         last name
      title
      code

Benefits of XML

  • XML follows an Open W3C standard
  • Data is represented across heterogeneous environments
  • Supports cross-platform use
  • High degree of interoperability
  • XML dictates strict rules
    • Syntax
    • Structure
    • Case sensitivity

XML Syntax Composition

  • An XML declaration (e.g., <?xml version = "1.0" encoding="ISO-8859-1"?>) is included though optional
  • There should be identification of the XML version, namespace, and encoding schema to which the document conforms
  • XML also contains Markup types
    • Elements (and attributes)
    • Entity references
    • Comments (<!-- what ever you want to say -->)
    • Processing instructions (<?instruction options?>)
  • Content

Elements and Attributes

  • Elements are tags
    • e.g. <course> </course>
  • Attributes are
    • <course level = "undergraduate">
  • Attributes provide information about an element
  • An attribute value is required, optional, or fixed, with a default value possible.
  • Attributes can often be replaced by nested elements.
  • All values must be quoted, even for numbers, as in XHTML.

Element and Attribute Naming

  • Elements and attributes can be named in nearly any manner.
  • These are descriptive and not confusing with no length limit
  • Are case sensitive, and can use the underscore (_).
  • No names are reserved for XML; namespaces resolve naming conflicts.
  • Names cannot start with a number, reserved punctuation, "XML" or "Xml".
  • Contain not spaces, colons (:), greater-than (>), or less-than (<)
  • Best practice to not use the hyphen (-) or period (.)

XML root element

  • Each element (lecturer, title, and code) is rooted in “course”
  • Each element (lecturer, title, and code) are children

XML Content

  • The text lies within elements
  • This is how documents are structured
  • Content may consist of any data as long as it does not get confused with XML metadata
  • Use Instructions use entity references for special characters!
  • Each XML document must have one root element

Well-Formed XML

  • XML must be formatted correctly
  • All tags must be closed

XML Namespaces

  • XML tags are user defined.
  • Elements with the name can lead to naming confllicts
  • XML applications use XML-based languages defined by various parties may have same element names.
  • Online furniture is good example. Vendors might have similar tables
  • To tackle table issues, use namespaces

XML namespace naming

<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
  • There will be no conflict because elements have different names

XML Namespace Properties

  • To use XML prefixes, a namespace must for the prefix itself must be defined
  • This is defined by the xmlns attribute in the start tag
  • This has the following syntax: xmlns:prefix="URI".
  • Each namespace is identified by a URI.

Namespaces: Declaration examples

xmlns: bk = "http://www.example.com/bookinfo/"
xmlns: bk = "urn:mybookstuff.org:bookinfo"
xmlns: bk = "http://www.example.com/bookinfo/"

Multiple XML Namespace

<furniture xmlns = "http://www.itee.uq.edu.au/~zxf/furniture"
xmlns:ikea = "http://www.ikea.com/names"
xmlns:html="http://www.w3.org/TR_REC-html40">
  • The code above shows the element can have 3 namespaces
    • One default, others for IKEA and HTML

Default and Scope

<BOOK xmlns="www.bookstuff.org/bookinfo">
<TITLE>All About XML</TITLE>
<AUTHOR>Joe Developer</AUTHOR>
<PUBLISHER xmlns="urn:publishers:publinfo">
<NAME>Microsoft Press</NAME>
</PUBLISHER>
</BOOK>
  • An XML namespace without a prefix becomes the default for sub-elements
  • Elements without a prefix are part of the default namespace
  • Unqualified elements belong to the inner-most default namespace
    • In the example BOOK, TITLE, and AUTHOR are part of book’s namespace
    • However, PUBLISHER and NAME fall under the publisher namespace

Sample XML document with two namespaces

<courselist xmlns:c="http://www.university.com/courses"
xmlns:l="http://www.university.com/lecturers">
    <c:course id="001">
        <c:name>SPDII</c:name>
        <l:lecturers>
            <l:lecturer>
                <l:fname>Sheron</l:fname>
                <l.Iname>Dinushka</I.Iname>
            </l:lecturer>           
            <l:lecturer>
                <l:name>Nishani</l:name>
                <l:Iname>Ranpatabandi</l:Iname>
            </l:lecturer>
        </l:lecturers>
    </c:course>
</courselist>

XML Schema (XSD)

  • XML Schema Definition language
  • Description of the structure of an XML document
    • XSD is an XML file following a fixed standard
    • It Replaces the less-flexible DTD (Document Type Definition)
  • Parsers use XD to check XML files; this is for validation as opposed to using well-formed structures:
    • With well-formed schemas: the general XML syntax rules are followed
      • e.g. Tags have end-tags, nesting is correct
    • Validity: The document follows XSD’s semantics
      • e.g. the tags/attributes used are defined in XSD, which means the structure is OK

What does XXSD Define?

  • Contains what elements can appear in a document
  • Dictates what attributes can appear
  • Which elements are children
  • Order of children
  • How many children
  • If the element can be empty, or include text
  • What elements are to have default and fixed values

Note.xml Example

<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com/note"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

XSD for Note.xml Sample

<?xml version="1.0"?>
<xs:schema xmlns:xs= "http://www.w3.org/2001/XMLSchema""
targetNamespace=“http://www.w3schools.com/note”
xmlns="http://www.w3schools.com"
elementFormDefault= "qualified">
    <xs:element name="note">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="to" type="xs:string"/>
                <xs:element name="from" type="xs:string"/>
                <xs:element name="heading" type="xs:string"/>
                <xs:element name="body" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Schema Element

  • The element is the root for every XML Schema.
  • The element can have attributes.
    xmlns:xs= http://www.w3.org/2001/XMLSchema
    
    -This states that the elements and data types are from “http://www.w3.org/2001/XMLSchema", which should be prefixed with xs

More on element

targetNamespace="http://www.w3schools.com/note"
  • This states that the elements defined by the schema (note, to, from, heading, body.) are assigned to the target namespace.
xmlns=http://www.w3schools.com
  • States the defualt namespace

elementFormDefault="qualified"

  • Elements used by the XML instance document must be namespace qualified if declared in this schema

Referencing XSD

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation="http://www.w3schools.com note.xsd">
  • This can be used to link the XML docs with the schemaLocation attribute
  • Since the schemaLocation attribute is in the "http://www.w3.org/2001/XMLSchema-instance"" namespace, this MUST be declared and it MUST be mapped to xsi as a prefix

Complex Types in XSD

  • A complex element is an XML element that contains other elements and/or attributes.
    • Example
  John
  Smith

  • The "employee" element can be declared directly by naming the element, like this:
  
      
           
           ""/>
          

Simple elements in XSD

  • Element has this syntax:
    • <xs:element name="xxx" type="yyy"/>
  • xxx is for element name, yyy is for type
  • XML has an array of built-in types, including:
    • string
    • decimal
    • integer
    • boolean
    • date
    • time

Defining data types example:

Smith
36
1970-03-27
  • Corresponding simple element definitions:
  
        type="xs:string"/>

        type="xs:integer"/>

        type="xs:date"/>

Attributes in XSD

  • Has this syntax:
    • <xs:attribute name="xxx" type="yyy"/>
  • xxx to attribute name, and yyy to specify data type
  • Simple elements DO NOT have attributes!

Attribute example

Smith

Structuring Data with XML for Stocks Example

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href=”StockPortfolio.css"?>
<StockPortfolio xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance"
xsi:schemaLocation="http://www.cs.curtin.edu.au/spd361
StockPortfolio.xsd">
  <Stocks>
    <StockPurchase>
      <Ticker>GOOG</Ticker>
      <PurchasePrice>330.06</PurchasePrice>
      <NumPurchased>30</NumPurchased>
    </StockPurchase>
    <StockPurchase>
      <Ticker>MSFT</Ticker>
      <PurchasePrice>17.21</PurchasePrice>
      <NumPurchased>580</NumPurchased>
    </StockPurchase>
  </Stocks>
</StockPortfolio >

Stock with XML Schema

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="StockPortfolio">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Stocks"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Stocks">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="StockPurchase" minOccurs="0"
        maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  </xs:schema>

More on Stock with XML Schema

<xs:element name="StockPurchase">
   <xs:complexType>
   <xs:sequence>
     <xs:element name="Ticker"/>
     <xs:element name="PurchasePrice"/>
     <xs:element name="NumPurchased"/>
   </xs:sequence>
   </xs:complexType>
  </xs:element>
  <xs:element name="Ticker" type="xs:string"/>
  <xs:element name="NumPurchased" type="xs:int"/>
  <xs:element name="PurchasePrice" type="xs:double"/>
</xs:schema>

Exercise: Write the XSD for the following XML

<?xml version = "1.0" encoding="ISO-8859-1"?>
<courselist>
    <course>
        <lecturer>
            <firstname> H.T. </firstname>
            <lastname> Shen </lastname>
        </lecturer>
        <title>SOA</title>
        <code>INFS3204</code>
    </course>
</courselist>

Automating XSD Creation

  • Although this example was created by hand
  • Tools existing make is automatic now
    • .NET: xsd.exe
    • Java: JAXB (Java Architecture for XML Binding) xjc.exe

Displaying XML

  • Displaying XML will require XSLT (extensible stylesheet language transformations)
  • Which is used to format XML documents
  • XML contains the content
  • XSLT contains the formatting
  • XML + XSLT = HTML

Text vs Binary Formats

  • "binary” format means the data concepts get mapped to the native Entities for the computer system; that means bytes
  • With the “text” format, the data concepts get mapped to characters which must be translated from or to computer entities
  • Strings can be needed for arithmetic

XML vs Binary

XML Binary
‘Human readable’ Requires software interpretation
Single, Universal Standard
  • Web-friendly
  • Massive commercial support
Many binary formats, availability varies
Stored as a stream of Unicode, in a single file
  • Organized as a tree
  • Cannot directly store ‘native’ numbers, must be encoded as unicode
Many organizations
  • store numbers in ‘native’ formats which are typically compact and require no translation in order to perform transfer

XML Advantages Over Binary

  • XML is self describing and also platform neutral

Binary Advantages Over XML

  • Size: Smaller data size, minimal overhead size (ie: no tags)
  • Speed: No need to convert between text and numeric data
  • Simpler: Parsing XML is a complicated exercise
  • And XML can be too flexible: multiple ways to do same thing

Why Text Over Binary

  • Encoded well for the network. Protocols such as data transfer are traditionally binary
    • Java is an example
  • XML formats data transmission entirely in text: XML tags
  • Such as SOAP. Basis for web services

What is Serialization?

  • Serialization is converting and encoding an object into a data stream
  • This includes aggregated or contained objects
  • Used for disk saving, or transfer over a network
  • Deserialization in conversion back from disk
  • Types Binary XML (single textual conversion)

Overview of JSON

  • What is JSON?
  • How does it compare with XML?
  • Discuss syntax
  • All data types
  • Usage

JSON is...

  • A lightweight data-interchange, text-based format
  • Language independent
  • Based on Javascript, therefore easy to understand

JSON is Not...

  • Overly Complex
  • a “document” format
  • a markup language
  • a programming language

Why Use JSON

  • Has straght foward syntax
  • IsEasy to create and manipulate
  • Can be parsed by JS eval() function
  • Supported by most JS frameworks

Does JSON compare with XML?

  • JSON and XML have similaries

  • Both are hierachical and good for data

  • JSON is lighter and faster

  • Unlike XML, JSON doesn't have

    • Namespaces
    • Inherit validation

JSON Object Syntax

  • These are unordered
  • Have name/value pairs
  • Follow these syntax requirements
    • Begins with { (left brace)
    • Ends with } (right brace)
    • Each name is followed by : (colon)
    • Name/value pairs are separated by , (comma)

JSON Example:

var employeeData = {
 "employee_id": 1234567,
 "name": "Jeff Fox",
 "hire_date": "1/1/2013",
 "location": "Norwalk, CT",
 "consultant": false
};

Arrays in JSON

  • Ordered collections of values.
  • Begins with [ (left bracket)
  • Ends with ] (right bracket).
  • Name/value pairs are separated by , (comma)

Boaleans and NULL

  • A boolean is either true or false
  • NuLL is something that doesn't represent a value

Types of Data Objects and Arrays

  • Objects: Unordered key/value pairs wrapped in { }
  • Arrays: Ordered key/value pairs wrapped in [ ]
  • You can nest objects (for supporting complex object)

Open Data Formats

  • YAML is an example of open data, or markup languages
  • Has these links to know more
    • https://blog.stackpath.com/yaml/
    • https://octopus.com/blog/state-of-config-file-formats
  • Often used to define REST APIs. Defines component/service interfaces and key usage.

Data Formats: Summary

  • Binary data formats are efficient to send and receive messages
  • However due TEXT datatypes, they are more open
  • XML therefore had richer features
  • However:
    • JSON is more lightweight
    • "You can also use YAML

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Services web: Concepts et Normes
15 questions
Web Systems and Technologies 2
37 questions

Web Systems and Technologies 2

CongenialLapSteelGuitar200 avatar
CongenialLapSteelGuitar200
Use Quizgecko on...
Browser
Browser