Professional Documents
Culture Documents
1 C# Language
STRUCTURED DATA
2
STRUCTURED DATA
3 CONCEPTS AND TERMINOLOGIES
CONCEPTS AND TERMINOLOGIES
○ Markup Languages: Languages used to define and structure the information
contained in a document
● HTML
● XML
7
XML
Based on 2 fundamental concepts
8
XML
Consequences of these two concepts: XML is...
○ A document format
● Extension: .xml
○ A data format
○ A metalanguage
● used to create other markup languages
9
<department deptid=“15”>
XML <deptname>…</deptname>
<employees>
XML vs. SQL <employee>
…
</employee>
<DEPARTMENTS>
<employee>
…
</employee>
</employees>
</department>
…
</DEPARTMENTS>
10
XML
XML vs. HTML
Compared to HTML, the tags in XML:
○ Have a meaning
○ Have a hierarchy
● structured data in tree structure
○ Are extensible
● defining new tags
12
XML
All XML documents:
document
○ "No": the application processor is expecting an external DTD reference
14
XML
Elements vs. Attributes
○ All tags without content must end with the characters "/>"
○The names of the start and end tags match exactly and in terms of case
● False: <Parent>Bob</parent>
● False: <Parent>Bob</child>
<child>Alice</ child> 16
</parent>
<result>a is < b</result>
XML
Basic Syntax Rules
○ Attribute names only appear once for each item Result:
○Attribute values are enclosed in quotes or apostrophes a<b
● <birthday date="2011-01-01">
● <birthday date='2011-01-01'>
○Avoid certain signs that could lead to confusion like -; . < > 17
● Instead, use predefined entities such as: < > …
XML
CDATA
○ By default, all data is PCDATA (Parsed Character Data): text parsed by a parser
● Tags will be taken in the context of markup
● The entities will be extended
○ CDATA allows you to define a block of characters that should not be parsed by
the XML parser:
● <! [CDATA [Welcome to this course class <XML and Distributed Software
Architecture>]]>
18
XML
Comments
○ Start with <!-- and end with -->
● False: <parent <! - this is not correct ->> Bob </ parent>
● True:
● <! - this is correct ->
● <child>Alice <! - this is correct -> </ child>
19
EXERCISE: ARE THE FOLLOWING XML DOCUMENTS WELL-FORMED?
<?xml version=“1.0”?>
<to>Tove</to>
(a) <from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<?xml version=“1.0”?>
<note>
(b) <to age=29>Tove</to>
<from>Jani</from>
</note>
21
EXERCISE: ARE THE FOLLOWING XML DOCUMENTS WELL-FORMED?
<?xml version=“1.0”?>
<breakfast menu>
<food price=“5.95” calories=“650” price=“5.90”>
<name>Belgian Waffles</nom>
(e)
<description>two of our famous Belgian Waffles</food>
</description>
</breakfast menu>
<?xml version="1.0"?>
<classe>
<titre>HTML Partie 1
(f) <periode nom="session 1">
<periode nom="session 2">
<periode nom="session 3">
</classe>
22
STRUCTURED DATA
23 JSON
JSON
○ JavaScript Object Notation
○ A data format
○ 2 types of elements
● object: set of key / value pairs
● array of objects
24
JSON
○ Compared to XML, JSON
● Lightweight: used to fetch data from a server
● Higher processing speed
27
JSON
{
"id": 8839,
"name": "Université Antonine",
"following": true,
"tweets": 1531,
…
"statuses": [
{
"created_at": "2015-03-21T17:32:00",
"id": 579334,
"text": "Bon travail pour vos examens! »,
"retweet_count": 17,
"favorite_count": 1,
},
{
"created_at": "2015-03-21T07:40:10",
"id": 579185,
"text": "Bienvenue à l’Université Antonine.",
"retweet_count": 0,
28
"favorite_count": 25
}
]
STRUCTURED DATA
29 Exercises
EXERCISE 1
○ Structure a "mind map" to elaborate data relevant to a restaurant menu
30
EXERCISE 2
Write the necessary data that are needed for
display in a view similar to the one on the right:
○ In XML format
○ In JSON format
31
EXERCICE 4 (SOLUTION)
{
<?xml version="1.0" encoding="UTF-8" ?> "users": [{
"id": 1,
<users> "fullname": "Ryan Hopkins",
<user id="1"> "likes": 178,
<fullname>Ryan Hopkins</fullname> "totalPosts": 13,
<likes>178</likes> "totalFollowing": 78,
<totalPosts>13</totalPosts> "totalFollowers": 65,
<totalFollowing>78</totalFollowing> "imageUrl": "http://sample.com/1.jpg",
<totalFollowers>65</totalFollowers> "city": "New York"
<imageUrl>http://sample.com/1.jpg</imageUrl> },
<city>New York</city> {
</user> "id": 2,
<user id="2"> "fullname": "John Doe",
<fullname>John Doe</fullname> "likes": 17,
<likes>17</likes> "totalPosts": 29,
<totalPosts>29</totalPosts> "totalFollowing": 13,
<totalFollowing>13</totalFollowing> "totalFollowers": 100,
<totalFollowers>100</totalFollowers> "imageUrl": "http://sample.com/2.jpg",
<imageUrl>http://sample.com/2.jpg</imageUrl> "city": "Paris"
<city>Paris</city> }
</user> ] 32
</users> }
XML PARSING IN JAVA
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(filePath);
private static Element createPersonElement(Document document, String name, String age, String city)
{
Element person = document.createElement("person");
person.appendChild(nameElement);
person.appendChild(ageElement);
person.appendChild(cityElement);
36
return person;
}
ADDING NEW ELEMENT AND SAVE IT