940 likes | 960 Views
Join this one-day XML programming workshop to learn about XML applications in B2B eCommerce and web development. Discover SAX, DOM, and practical labs. No prior experience required. URLs and industry insights included.
E N D
XML Programming Thomas Lee E-Mail: thomas_c_lee@yahoo.com
Course Description • This one-day workshop on XML programming will help you learn about the XML programming in B2B eCommerce and the implication of XML for web site development. XML (eXtensible Markup Language) defines a universal standard for electronically exchanging data for the B2B e-Commerce. It is supported by major IT vendors. Topics covered include: Background review of XML, Introduction to SAX, DOM and their usage. Simple Labs will be provided.
Course Outline • XML Overview • SAX • DOM • Lab 1 • Lab 2 • Lab 3
References • Books • “Java and XML” Brett McLaughhlin, O’Reilly • Online resources: • http://www.xmlspy.com • http://www.w3.org/TR/xmsg/ • http://www.w3.org/TR/xmlschema-1 • http://www.w3org/TR/REC-xml-names • http://xml.apache.org • http://www.alphaworks.ibm.com/tech/LotusXSL • http://technet.oracle.com/tech/xml
Expected Background • . Basic Knowledge of HTML and XML. • Basic Programming skill, such as visual • Basic, C++ or Java is a Plus !!!
Course Schedule • 9:00 AM ~ 9:50 AM Overview of XML and its usage in E-Commerce • 10:00 AM~10:50 AM Introduction to SAX Programming (Simple XML API) • 11:00 AM ~ 11:50 AM Lab1 • 11:50 ~ 12:50 PM Introduction to DOM Programming (Microsoft and Sun) • 1:00 PM ~ 1:50 PM Lab2 • 2:00 PM ~ 2:50 PM XML used in eCommerce Environment • 3:00 PM ~ 4:00 PM Lab 3
Logistics • Parking Permit • Rest Room • Café Tear
Course Instructor • Thomas Lee • Master in both Computer Science and Electronics Engineer. • Over 17 years industry experience. • Has extensive experience in eCommerce Application. • Senior Consultant in Telecommunication Industry.
Attendee Introductions • Your Name • Background • Why you take this course ? • What you expect from this course ?
Quick Review of XML Background • Background • In 1996, A new team of experts led by Jon Bosak of Sun Microsystems and backed by W3 began to work on a new standard. • Finalized in February in 1998 called XML. • XML( eXtensible Markup Language) is derived from SGML ( Standard Generalized Markup Language). • The percentage of IT shops in the US using XML in Web jumped from 1% to 16%.
Quick Review of XML Background • Background • A typical enterprise will use 35-40 percent of its programming budget in developing data extraction and updating programs for the sole purpose of information transfer between different databases and applications, XML is the best candidate for those purpose. • The eXtensible Markup Language(XML) has been developed by the World Wide Web Consortium(W3C) XML Working Group to bring SGML (Standard Generalized Markup Language) to the Web.
Quick Review of XML Background • Background • SGML is a language for the specification of markup languages. SGML is parent of the well-known HyperText Markup Language(HTML). • XML's design was conducted by looking at the strengths and weaknesses of SGML but without all the complex and rarely used features. • The first working draft for XML was published in November 1996.
Quick Review of XML Background • Why XML • Simple, • open, • self-describing, • provides a basis for author identification and versioning at the element level. • Machine-readable context information • Separates content from presentation • Supports multilingual documents • Facilitates the comparison and aggregation of data
Quick Review of XML Background • Why XML • Can embed multiple data types -- can contain any possible data type • form multimedia data (image, sound, video) to active components (Java applets) • Can embed existing data • ‘One server’ view for distributed data • Wide-spread acceptance in the industry.
XML Technologies Definition and Structure XML Data Types XML Schema DTD Data Store Display and Reporting XML XSL Data Transfer Application HTML/CSS Link to other resources XPL XLL HTML XML Resources
Java and XML • XML is an essential component in the Java 2 Platform, Enterprise Edition (J2EETM) and will be supported throughout the J2EE platform as a means for enabling business-to-business information interchange using XML. • For robust, synchronous data messaging, Enterprise JavaBeansTM (EJBTM) architecture-based components (enterprise beans) can be used to create a business service object, and related XML content can be published on the web using JavaServer PagesTM technology. Currently, JavaServer Pages technology can be used to generate and consume XML between n-tier servers or between server and client.
Java and XML • Java Messaging Service provides a means for asynchronous XML data messaging. In addition, EJB technology uses XML to describe its deployment properties, giving enterprise beans data portability in addition to their code portability.
Quick Review of XML Background • XML Components • Header • Instruction e.g. <?xml version='1.0' encoding='UTF-8' ?> • Document Type Declaration e.g <!DOCTYPE Course SYSTEM "Demo1.dtd"> • Content • Root Element, Data Element e.g <Course>………</Course> • Element Attributes e.g <Course Semester=‘Spring’> • References e.g. <Price>&onedayPrice</Price> • Unparsed Data e.g.<![CDATA[ sdfdsff <>s<<<>> ]]>
Quick Review of XML Background Well-Formed XML • Must start with an XML declaration • Non-empty tags must match and nest properly • Empty tags must end with /> • Must have exactly one root element • Tag and attribute names are case-sensitive • Attribute values must be quoted
Quick Review of XML Background Rules for Well-Formed XML(1) • Mandatory Closing Tags The set of Tags is unlimited but all container tags must have end tags. <tag>…</tag> <TAG>…</TAG> • Example of legal XML • <Person><Name>Adam Smith</Name> <Title>General Manager</Title> <Age>42</Age></Person>
Quick Review of XML Background Rules for Well-Formed XML(2) • Proper Element Nesting. All tags must be nested correctly. Like HTML, XML can intermix tags and text, but tags may not overlap each other. • Legal XML <Person>John Smith is an <role>advocate </role> for <technology>XML</technology> </Person> • Illegal XML <Person><Name>John </Name><KeyPoint><Hd>XML provides a data bus</Hd> </Person><More>…</More> </KeyPoint>
Quick Review of XML Background Rules for Well-Formed XML(3) • Double-quote value delimiters All attribute values must be enclosed in single or double quotation marks. • Legal: <tag attribute = "value"> or <tag attribute = 'value'> • Illegal: <font size = 6> XML tags are case sensitive. <myTAG> <Mytag> are different
Quick Review of XML Background Rules for Well-Formed XML(4) • Single tag elements. Singleton tags (called empty element or tags without content) must be written in an abbreviated form using special XML syntax. • Legal: <BR/> <HR/> • <TITLE></TITLE> is equivalent to <TITLE/> • Illegal: <BR> <HR>
Quick Review of XML Background XML Document: Validation Two kinds of ways to define rules for the document. These rules can be used to validate an XML document or used by XML authoring tools to guide the creation of an XML document. • Document Type Definition (DTD): This is used to define a grammar for the tags and attributes. This syntax is supported, but deprecated by Microsoft. It uses a special non-XML-based grammar • XML Schema (XML-Data): This is a much richer and more extensible way to describe the rules for the content of a document and uses XML itself as a grammar.
Quick Review of XML Background Keyword Implication #REQUIRED The element must have a value for the attribute specified. If the value is not specified, the document will not be processed #FIXED The attribute has a value that does not change throughout the document. #IMPLIED The attribute may have a value declared, or it could be left empty. Default Value This value is specified in place of a keyword in the <!ATTLIST> tag. If a value is specified in the document, the default value is replaced. However, if the document leaves the attribute value blank, then the default value is assumed.
Quick Review of XML Background Sample of DTD ( Embedded) <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE Course [ <!ELEMENT Title (#PCDATA)> <!ELEMENT Price (#PCDATA)> <!ELEMENT Instructor (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT Course (Title, Price, Instructor, Date) > ]> <Course> <Title>XML Programming</Title> <Price>$275</Price> <Instructor>AIT</Instructor> <Date>March 17,2001</Date> </Course>
Quick Review of XML Background Sample of DTD (Reference) Demo1.dtd <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE Course [ <!ELEMENT Title (#PCDATA)> <!ELEMENT Price (#PCDATA)> <!ELEMENT Instructor (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT Course (Title, Price, Instructor, Date) > ]>
Quick Review of XML Background Sample of DTD (Reference) <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE Course [<?SYSTEM “\DTD\Demo1.dtd”? >] > <Course> <Title>XML Programming</Title> <Price>$275</Price> <Instructor>AIT</Instructor> <Date>March 17,2001</Date> </Course>
Quick Review of XML Background XML Schema • XML Schema is designed to replace and amplify DTDs ( because DTD is a strange animal in XML community) • It offers an XML-centric means to constrain XML documents. • Reference: http://www.w3.org/TR/xmlschema-1
Quick Review of XML Background • Well-Formed XML: • A well-formed XML document does not necessarily include a DTD or a schema. • Valid XML: • XML that conforms to the vocabulary specified in a DTD or schema.
Quick Review of XML Background Namespaces • A mapping between an element prefix and a URI • Is used for handling namespace collisions and defining data structures that allow parsers to handle collisions. • Examples <Airplane:Engine> <Car:Seat> • http://www.w3org/TR/REC-xml-names
Quick Review of XML Background XSL and XSLT • XSL ( Extensible Stylesheet Language) • Transforms and translates XML data from one XML format to another format. • XSLT ( Extensible Stylesheet Language Transformation) • A Complete Text-based transformation process. • Reference: • Apache Xalan: http://xml.apache.org • Lotus XSL Processor: http://www.alphaworks.ibm.com/tech/LotusXSL • Oracle XSL Processor:http://technet.oracle.com/tech/xml
Quick Review of XML Background XSL and XML Sample greeting.xml <?xml version="1.0" standalone="yes"?> <?xml-stylesheet href="greeting.xsl" type="text/xsl" ?> <greeting> Hello World! </greeting> greeting.xsl <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <H1> <xsl:value-of /> </H1> </xsl:template> </xsl:stylesheet>
Quick Review of XML Background Xpath • The XPath Recommendation defines a path language and expression syntax used by XSLT, XPointer, and XLink, it defines how a specific item within an XML document can be located. • An XML document is considered a tree of nodes ( any piece of XML data, including elements, attributes, and textual data), where each node can be accessed by specifying the location in the tree at which it is located. • Reference: http://www.w3.org/TR/xpath
Quick Review of XML Background SAX • It is Simple API for XML • It provides the means to parse a document. • It provides an event-based framework for parser to parse XML data. • Available parsers: • Sun’s Project x • Oracle’s XML parser • Apache’s Xerces • IBM’s XML4J • Reference: http://www.w3.org/DOM
Quick Review of XML Background DOM • It is an API for the Document Object Model • It provides a means of manipulating the XML data. • It provides a representation of an XML document as a tree. • Reference: http://www.w3.org/DOM
Quick Review of XML Background SAX/DOM
Quick Review of XML Background XML Application: • Presentation ( XML + XSL) • Communication (HTTP/XML, XML-PRC) • Configuration • EJB ( deployment descriptor) • Servlet API version 2.2.
Quick Review of XML Background XML IDE & Editors Examples: • Microsoft XML Notepad ( editor) • XML Spy 3.5(Editor & IDE) • XML editing & validation • Schema/DTD editing & validation • XSL editing & transformation • Reference: http://www.xmlspy.com
SAX Programming In Java
SAX Programming • Guidelines • (1) Finding a Parser • e.g. Apache Xerces parser • http://xml.apache.org • (2) Make sure which SAX Version: 1.0/2.0 • We will use SAX 1.0 in our Lab • (3) Make sure the following classes( packages) in your jar files ( we use utils.jar in our lab) org.xml.sax, org.xml.sax.helper, javax.xml.parser, com.sun.xml.parser com.sun.xml.tree
SAX Programming • Guidelines • (4)Prepare a XML document • (5)Write Programs to parse XML document.
SAX Programming • Run your first program • Run Script: SAXDemo1\Bulid.bat • Java Program: SAXDemo1\SAXDemo1.java • XML Document: SAXDemo1\ Demo0.xml SAXDemo1\ Demo1.xml SAXDemo1\ Demo2.xml SAXDemo1\ Demo1.dtd • Test Case 1: No DTD Validation • Test Case 2: DTD Validation
SAX Programming • SAX Related package • org.xml.sax • org.xml.sax.helpers
SAX Programming-SAXDemo1.java Import necessary libraries • java.io.* • org.xml.sax.* • java.util.*; • javax.xml.parsers.SAXParserFactory • javax.xml.parsers.ParserConfigurationException • javax.xml.parsers.SAXParser API Interface Parser
SAX Programming-SAXDemo1.java • org.xml.sax : -Defines the SAX interfaces • org.xml.sax.helpers • It defines the ParserFactory class, which lets you acquire an instance of a parser either by specifying a name string or by using the value defined by the org.xml.sax.parser system property. • javax.xml.parsers • Defines the SAXParserFactory class which returns the SAXParser • com.sun.xml.parser • Contains the Java XML parser (com.sun.xml.parser.Parser), • validating parser (com.sun.xml.parser.ValidatingParser), and entity resolver
SAX Programming-SAXDemo1.java Contents in org.xml.sax package Interfaces AttributeList Attributes ContentHandler DocumentHandler DTDHandler EntityResolver ErrorHandler Locator Parser XMLFilter XMLReader Classes HandlerBase InputSource For SAX 1.0 Exceptions SAXException SAXNotRecognizedException SAXNotSupportedException SAXParseException Bold: for SAX 2.0
SAX Programming-SAXDemo1.java Contents in org.xml.sax.helpers package Classes AttributeListImpl AttributesImpl DefaultHandler LocatorImpl NamespaceSupport ParserAdapter ParserFactory XMLFilterImpl XMLReaderAdapter XMLReaderFactory Bold for SAX 2.0
SAX Programming-SAXDemo1.java Creating /Setting Parser Object/Features try{ SAXParserFactory mfactory = SAXParserFactory.newInstance(); mfactory.setValidating(bValidate); if (mfactory!=null){ SAXParser saxParser = mfactory.newSAXParser(); saxParser.parse( new File(fileName), this); } } catch (SAXParseException spe) {…} catch(……){…} Set DTD Validating Feature Create SAXParser Read from File Useful if we set validating to true
SAX Programming-SAXDemo1.java Implements ContentHandler Functions(0) • SAX parser generates the following linear events: • setDocumentLocator • start document • start element • characters • end element • end document