SlideShare une entreprise Scribd logo
1  sur  21
The eXtensible Markup Language
(XML)
Presentation Outline
 Part 1: The basics of creating an XML
document
 Part 2: Developing constraints for a well
formed XML document
 Part 3: XML and supplementary technologies
Part 1: Background for XML
 An Extensible Markup Language (XML)
document describes the structure of data
 XML and HTML have a similar syntax …
both derived from SGML
 XML has no mechanism to specify the format
for presenting data to the user
 An XML document resides in its own file with
an ‘.xml’ extension
Main Components of an XML
Document
 Elements: <hello>
 Attributes: <item id=“33905”>
 Entities: &lt; (<)
 Advanced Components
– CData Sections
– Processing Instructions
An Example XML Document
 An example of an well-commented XML document
The Basic Rules
 XML is case sensitive
 All start tags must have end tags
 Elements must be properly nested
 XML declaration is the first statement
 Every document must contain a root element
 Attribute values must have quotation marks
 Certain characters are reserved for parsing
Common Errors for Element Naming
 Do not use white space when creating
names for elements
 Element names cannot begin with a digit,
although names can contain digits
 Only certain punctuation allowed – periods,
colons, and hyphens
Walking through an Example
 Modify the computer.xml document
– Add a new element named “software” with an attribute
named “language”
– The attribute’s value should be the name of a programming
language
– Create another XML element called “IFStatment”
– Use the IFStatment element to tag the following data:
if (a < b && b >= 0)
– Close the “software” tag
 After you have added these new items into the XML
document, parse it again to ensure that it is still well
formed. Use the feedback to correct any errors.
Part 2: Legal Building Blocks of XML
 A Document Type Definition (DTD) allows the
developer to create a set of rules to specify legal
content and place restrictions on an XML file
 If the XML document does not follow the rules
contained within the DTD, a parser generates an
error
 An XML document that conforms to the rules within a
DTD is said to be valid
Why Use a DTD?
– A single DTD ensures a common format for each
XML document that references it
– An application can use a standard DTD to verify
that data that it receives from the outside world is
valid
– A description of legal, valid data further
contributes to the interoperability and efficiency of
using XML
Some Example DTD Declarations
 Example 1: The Empty Element
<!ELEMENT Bool (EMPTY)> <!--DTD declaration of empty
element-->
<Bool Value="True"></Bool> <!--Usage with attribute in XML
file-->
 Example 2: Elements with Data
<!ELEMENT Month (#PCDATA)> <!--DTD declaration of an element->
<Month>April</Month> <!—Valid usage within XML file-->
<Month>This is a month</Month> <!—Valid usage within XML file-->
<Month> <!—Invalid usage within XML file, can’t have children!-->
<January>Jan</January>
<March>March</March>
</Month>
Some Example DTD Declarations
 Example 3: Elements with Children
To specify that an element must have a single child element, include the element name within the
parenthesis.
<!ELEMENT House (Address)> <!—A house has a single address-->
<House> <!—Valid usage within XML file-->
<Address>1345 Preston Ave Charlottesville Va 22903</Address>
</House>
An element can have multiple children. A DTD describes multiple children using a sequence, or a
list of elements separated by commas. The XML file must contain one of each element in the
specified order.
<!--DTD declaration of an element-->
<!ELEMENT address (person,street,city, zip)>
<!ELEMENT person (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
<!—Valid usage within XML file-->
<address>
<person>John Doe</person>
<street>1234 Preston Ave.</street>
<city>Charlottesville, Va</city>
<zip>22903</zip>
</address>
Cautions concerning DTDs
 All element declarations begin with <!ELEMENT
and end with >
 The ELEMENT declaration is case sensitive
 The programmer must declare all elements within
an XML file
 Elements declared with the #PCDATA content
model can not have children
 When describing sequences, the XML document
must contain exactly those elements in exactly that
order.
Walking Through an Example
 Using the file “music.xml” contained in the extras
folder, create a Document Type Definition that
describes all of the elements. The goals for this
exercise are to:
– map out the elements
– define each element
– pick the best content model for each element
– correctly order the elements using sequences
 Internally embed the DTD within the XML document
Part 3: XML and Supplementary
Technologies
 The W3C Document Object Model (DOM)
– an API that allows developers to programmatically manage
and access XML nodes
– allows programmers to update and change XML documents
within an application
– reads the whole XML file and then stores a hierarchical tree
structure containing all elements within the document
– This tree has a single root node, which is the root element,
and may contain many children, each of which represents
an XML element
W3C DOM with JavaScript
 Example 1: Loading the XML document: DOMDocument
• The programmer can use a Microsoft Active X object to
parse an XML file
//Instantiate DOMDocument object
var XMLfile = new ActiveXObject("Msxml2.DOMDocument");
XMLfile.load("newspaper.xml");
var rootElement = XMLfile.documentElement;
document.write("The root node of the XML file is: ");
document.writeln("<b>" + rootElement.nodeName
+"</b>");
W3C DOM with JavaScript
 Example 2: Accessing the Children Elements
• The childNodes member of any element node gives the
programmer access to all of the sibling nodes of that
element
//traverse through each child of the root element
//and print out its name
for (i=0; i<rootElement.childNodes.length; i++) {
var node = rootElement.childNodes.item(i);
document.write("The name of the node is ");
document.write("<b>" + node.nodeName + "</b>");
}
W3C DOM with JavaScript
 Example 3: Getting Element Attributes
//traverse through each child of the root element
//and print out its name
for (i=0; i<rootElement.childNodes.length; i++) {
//get the current element
var elementNode = rootElement.childNodes.item(i);
document.writeln("Processing Node: " +
elementNode.nodeName + "<BR>");
var attributeValue;
//get an attribute value by specific name
attributeValue = elementNode.getAttribute("articleID");
//print it out
document.writeln("Attribute value: <b>" + attributeValue +
" </b><br>");
}
Cautions with DOM
 Make sure that the XML file resides in the same directory as
the html file with the JavaScript code
 The Attribute node does not appear as the child node of any
other node type; it is not considered a child node of an
element
 Use caution when outputting raw XML to Internet Explorer.If
the programmer uses the document.writeln method, IE
attempts to interpret the XML tags and jumbles the text.
Instead, use an alert box when debugging.
Walking through an Example
1. Create an HTML file with notepad. Insert
some JavaScript code that will parse
newspaper.xml into a DOM tree. Print out
the attribute values for each articleID.
Questions

Contenu connexe

Tendances (19)

01 xml document structure
01 xml document structure01 xml document structure
01 xml document structure
 
XML
XMLXML
XML
 
Dtd
DtdDtd
Dtd
 
Intro xml
Intro xmlIntro xml
Intro xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Xml dtd
Xml dtdXml dtd
Xml dtd
 
Xml presentation
Xml presentationXml presentation
Xml presentation
 
Extensible Markup Language (XML)
Extensible Markup Language (XML)Extensible Markup Language (XML)
Extensible Markup Language (XML)
 
Xml dtd
Xml dtdXml dtd
Xml dtd
 
EXtensible Markup Language
EXtensible Markup LanguageEXtensible Markup Language
EXtensible Markup Language
 
DTD
DTDDTD
DTD
 
XML
XMLXML
XML
 
XML
XMLXML
XML
 
Building XML Based Applications
Building XML Based ApplicationsBuilding XML Based Applications
Building XML Based Applications
 
Xml dtd- Document Type Definition- Web Technology
Xml dtd- Document Type Definition- Web TechnologyXml dtd- Document Type Definition- Web Technology
Xml dtd- Document Type Definition- Web Technology
 
DTD
DTDDTD
DTD
 
02 xml schema
02 xml schema02 xml schema
02 xml schema
 
uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2
 
03 namespace
03 namespace03 namespace
03 namespace
 

En vedette (12)

5 fifth lesson -xml
5 fifth lesson -xml5 fifth lesson -xml
5 fifth lesson -xml
 
Xml plymouth
Xml plymouthXml plymouth
Xml plymouth
 
Course index
Course indexCourse index
Course index
 
Xml1111
Xml1111Xml1111
Xml1111
 
XML in Libraries
XML in LibrariesXML in Libraries
XML in Libraries
 
XML.ppt
XML.pptXML.ppt
XML.ppt
 
Xml
XmlXml
Xml
 
Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
 
Xml ppt
Xml pptXml ppt
Xml ppt
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
XML - What is XML?
XML - What is XML?XML - What is XML?
XML - What is XML?
 
Tutorial XML
Tutorial XMLTutorial XML
Tutorial XML
 

Similaire à Xml11 (20)

Xml
XmlXml
Xml
 
XML.ppt
XML.pptXML.ppt
XML.ppt
 
XML-Unit 1.ppt
XML-Unit 1.pptXML-Unit 1.ppt
XML-Unit 1.ppt
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
Unit 5 xml (1)
Unit 5   xml (1)Unit 5   xml (1)
Unit 5 xml (1)
 
Tp2
Tp2Tp2
Tp2
 
M.FLORENCE DAYANA WEB DESIGN -Unit 5 XML
M.FLORENCE DAYANA WEB DESIGN -Unit 5   XMLM.FLORENCE DAYANA WEB DESIGN -Unit 5   XML
M.FLORENCE DAYANA WEB DESIGN -Unit 5 XML
 
distributed system concerned lab sessions
distributed system concerned lab sessionsdistributed system concerned lab sessions
distributed system concerned lab sessions
 
XML notes.pptx
XML notes.pptxXML notes.pptx
XML notes.pptx
 
Xml
XmlXml
Xml
 
WEB PROGRAMMING
WEB PROGRAMMINGWEB PROGRAMMING
WEB PROGRAMMING
 
Xml2
Xml2Xml2
Xml2
 
Intro to xml
Intro to xmlIntro to xml
Intro to xml
 
Introduction to xml schema
Introduction to xml schemaIntroduction to xml schema
Introduction to xml schema
 
xml introduction in web technologies subject
xml introduction in web technologies subjectxml introduction in web technologies subject
xml introduction in web technologies subject
 
Xml intro1
Xml intro1Xml intro1
Xml intro1
 
Web Technologies Unit 2 Print.pdf
Web Technologies Unit 2 Print.pdfWeb Technologies Unit 2 Print.pdf
Web Technologies Unit 2 Print.pdf
 
Xml session
Xml sessionXml session
Xml session
 
Xml 1
Xml 1Xml 1
Xml 1
 
IT6801-Service Oriented Architecture
IT6801-Service Oriented ArchitectureIT6801-Service Oriented Architecture
IT6801-Service Oriented Architecture
 

Plus de Sudharsan S (20)

Xml
XmlXml
Xml
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Unix
UnixUnix
Unix
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
Unix
UnixUnix
Unix
 
C Lecture
C LectureC Lecture
C Lecture
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
C Introduction
C IntroductionC Introduction
C Introduction
 
College1
College1College1
College1
 
C Programming
C ProgrammingC Programming
C Programming
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Preface
PrefacePreface
Preface
 
Toc Sg
Toc SgToc Sg
Toc Sg
 
Les08
Les08Les08
Les08
 
Les06
Les06Les06
Les06
 
Les07
Les07Les07
Les07
 
Les04
Les04Les04
Les04
 
Les05
Les05Les05
Les05
 
Les03
Les03Les03
Les03
 
Les02
Les02Les02
Les02
 

Dernier

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 

Dernier (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 

Xml11

  • 1. The eXtensible Markup Language (XML)
  • 2. Presentation Outline  Part 1: The basics of creating an XML document  Part 2: Developing constraints for a well formed XML document  Part 3: XML and supplementary technologies
  • 3. Part 1: Background for XML  An Extensible Markup Language (XML) document describes the structure of data  XML and HTML have a similar syntax … both derived from SGML  XML has no mechanism to specify the format for presenting data to the user  An XML document resides in its own file with an ‘.xml’ extension
  • 4. Main Components of an XML Document  Elements: <hello>  Attributes: <item id=“33905”>  Entities: &lt; (<)  Advanced Components – CData Sections – Processing Instructions
  • 5. An Example XML Document  An example of an well-commented XML document
  • 6. The Basic Rules  XML is case sensitive  All start tags must have end tags  Elements must be properly nested  XML declaration is the first statement  Every document must contain a root element  Attribute values must have quotation marks  Certain characters are reserved for parsing
  • 7. Common Errors for Element Naming  Do not use white space when creating names for elements  Element names cannot begin with a digit, although names can contain digits  Only certain punctuation allowed – periods, colons, and hyphens
  • 8. Walking through an Example  Modify the computer.xml document – Add a new element named “software” with an attribute named “language” – The attribute’s value should be the name of a programming language – Create another XML element called “IFStatment” – Use the IFStatment element to tag the following data: if (a < b && b >= 0) – Close the “software” tag  After you have added these new items into the XML document, parse it again to ensure that it is still well formed. Use the feedback to correct any errors.
  • 9. Part 2: Legal Building Blocks of XML  A Document Type Definition (DTD) allows the developer to create a set of rules to specify legal content and place restrictions on an XML file  If the XML document does not follow the rules contained within the DTD, a parser generates an error  An XML document that conforms to the rules within a DTD is said to be valid
  • 10. Why Use a DTD? – A single DTD ensures a common format for each XML document that references it – An application can use a standard DTD to verify that data that it receives from the outside world is valid – A description of legal, valid data further contributes to the interoperability and efficiency of using XML
  • 11. Some Example DTD Declarations  Example 1: The Empty Element <!ELEMENT Bool (EMPTY)> <!--DTD declaration of empty element--> <Bool Value="True"></Bool> <!--Usage with attribute in XML file-->  Example 2: Elements with Data <!ELEMENT Month (#PCDATA)> <!--DTD declaration of an element-> <Month>April</Month> <!—Valid usage within XML file--> <Month>This is a month</Month> <!—Valid usage within XML file--> <Month> <!—Invalid usage within XML file, can’t have children!--> <January>Jan</January> <March>March</March> </Month>
  • 12. Some Example DTD Declarations  Example 3: Elements with Children To specify that an element must have a single child element, include the element name within the parenthesis. <!ELEMENT House (Address)> <!—A house has a single address--> <House> <!—Valid usage within XML file--> <Address>1345 Preston Ave Charlottesville Va 22903</Address> </House> An element can have multiple children. A DTD describes multiple children using a sequence, or a list of elements separated by commas. The XML file must contain one of each element in the specified order. <!--DTD declaration of an element--> <!ELEMENT address (person,street,city, zip)> <!ELEMENT person (#PCDATA)> <!ELEMENT street (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT zip (#PCDATA)> <!—Valid usage within XML file--> <address> <person>John Doe</person> <street>1234 Preston Ave.</street> <city>Charlottesville, Va</city> <zip>22903</zip> </address>
  • 13. Cautions concerning DTDs  All element declarations begin with <!ELEMENT and end with >  The ELEMENT declaration is case sensitive  The programmer must declare all elements within an XML file  Elements declared with the #PCDATA content model can not have children  When describing sequences, the XML document must contain exactly those elements in exactly that order.
  • 14. Walking Through an Example  Using the file “music.xml” contained in the extras folder, create a Document Type Definition that describes all of the elements. The goals for this exercise are to: – map out the elements – define each element – pick the best content model for each element – correctly order the elements using sequences  Internally embed the DTD within the XML document
  • 15. Part 3: XML and Supplementary Technologies  The W3C Document Object Model (DOM) – an API that allows developers to programmatically manage and access XML nodes – allows programmers to update and change XML documents within an application – reads the whole XML file and then stores a hierarchical tree structure containing all elements within the document – This tree has a single root node, which is the root element, and may contain many children, each of which represents an XML element
  • 16. W3C DOM with JavaScript  Example 1: Loading the XML document: DOMDocument • The programmer can use a Microsoft Active X object to parse an XML file //Instantiate DOMDocument object var XMLfile = new ActiveXObject("Msxml2.DOMDocument"); XMLfile.load("newspaper.xml"); var rootElement = XMLfile.documentElement; document.write("The root node of the XML file is: "); document.writeln("<b>" + rootElement.nodeName +"</b>");
  • 17. W3C DOM with JavaScript  Example 2: Accessing the Children Elements • The childNodes member of any element node gives the programmer access to all of the sibling nodes of that element //traverse through each child of the root element //and print out its name for (i=0; i<rootElement.childNodes.length; i++) { var node = rootElement.childNodes.item(i); document.write("The name of the node is "); document.write("<b>" + node.nodeName + "</b>"); }
  • 18. W3C DOM with JavaScript  Example 3: Getting Element Attributes //traverse through each child of the root element //and print out its name for (i=0; i<rootElement.childNodes.length; i++) { //get the current element var elementNode = rootElement.childNodes.item(i); document.writeln("Processing Node: " + elementNode.nodeName + "<BR>"); var attributeValue; //get an attribute value by specific name attributeValue = elementNode.getAttribute("articleID"); //print it out document.writeln("Attribute value: <b>" + attributeValue + " </b><br>"); }
  • 19. Cautions with DOM  Make sure that the XML file resides in the same directory as the html file with the JavaScript code  The Attribute node does not appear as the child node of any other node type; it is not considered a child node of an element  Use caution when outputting raw XML to Internet Explorer.If the programmer uses the document.writeln method, IE attempts to interpret the XML tags and jumbles the text. Instead, use an alert box when debugging.
  • 20. Walking through an Example 1. Create an HTML file with notepad. Insert some JavaScript code that will parse newspaper.xml into a DOM tree. Print out the attribute values for each articleID.