aide's Blog
Introduction
The XmlTextReader class is not the most intuitive class to work with, as the methods and properties are very low level. While the XmlTextReader class is rich in properties and methods, I've found that most of what it provides isn't necessary for the average day-to-day job. So, in this article, I'm going to present a moderately thin wrapper class for the XmlTextReader, which should be a helpful guide to using the XmlTextReader for programmers not familiar with this class. This article is also an introduction to a variety of other disciplines that I feel a beginner should be aware of--code commenting, abstraction and architecture, and unit tests. So, hopefully, there's something here for everybody! Why Use an XmlReader?To summarize from this site:
Naturally, XmlTextReader is closer to the XML. It is a forward only reader, meaning that you can't go backwards. Contrast this with the XmlDocument class, which pulls in the entire document and allows you to random-access the XML tree. The XmlTextReader supports streams, and therefore should reduce memory requirements for very large documents. Another advantage of the XmlTextReader is that it provides line and character position information, which can be very useful diagnostic information when there's a problem with the XML. What Features Do I Want to Support?There's a core set of features in XML that I want my reader to support:
There are other features of XML, but these are the most common ones, and the ones I want to start with. The link cited above demonstrates a somewhat different approach than I am doing here, and it's useful to briefly discuss the difference. In the SoftArtisans link, many of the code snippets demonstrate looking for and evaluating specific elements and attributes of the XML, including optional ones. In other words, the application has expectations regarding the XML graph and content. The reader that I am presenting is tailored more to processing ad hoc XML, where there are no expectations regarding the graph and the content. Both approaches have there value depending on what you need to get accomplished. The Unit TestsThe unit tests are written for my Advanced Unit Test application, downloadable here. The reason I'm using my unit test application instead of NUnit is because I want to take advantage of AUT's ability to execute tests in sequence, as I read through the XML. Yes, I could have instead written an XML fragment for each unit test, but I find this more convenient and more realistic, as I can work with the entire XML document. Here's the XML test document, which illustrates each of the features described above: Something this simple doesn't need an architecture, does it? In fact, it does. Even with something this simple, it's a good idea to consider what abstraction you might want (planning for the future) and helper objects that will make understanding and working with the code easier. And of course, we need to consider what kind of exceptions the reader will throw. As a side comment, it always surprises me how a good architecture, even for the simplest of functionality, practically eliminates monolithic code and helps to create nice small methods that are easily unit tested. I potentially want to read formats other than XML, while staying within the constraints of an XML-ish structure. For example, a comma separated value file (CSV) is a good candidate for an alternative reader implementation. By abstracting the reader, I can support alternative formats without having to change the code that uses the reader. This is a design decision that is best made early on. The reader implements an IReader interface that provides the necessary abstraction layer: Since this is a beginning article, I want to emphasize something here--there is no excuse for not putting in at least basic comments in your code. None. It is a discipline that I myself have worked hard to achieve, but if you're writing a professional application that you or others may one day need to maintain, you simply have to force yourself to become disciplined about writing comments. The interface:
Anyone interested in implementing a custom reader now knows what the custom reader needs to implement. An application needing a reader can now reference the reader via the IReader interface, and a factory pattern can be used to instantiate the appropriate reader. The Container ClassesThere are several container classes that help encapsulate information relevant to all nodes and relevant to specific nodes. Creating classes that encapsulate fields improves code readability and provides a layer of separation from the underlying implementation (the Reader class, in this case). And no, none of the container classes are unit tested--you have to draw the line somewhere, and these classes are much too simple to spend the time on unit testing. LineInfoAll XML nodes have line and character position information, which is encapsulated in the LineInfo class: Since this class is instantiated strictly by the reader, the properties are read-only. NodeInfoNodeInfo is an abstract class that encapsulates the two common elements of just about every XML node (there are a few exceptions): the node name and the node prefix. It's an abstract class because we want to make sure that the implementation utilizes an appropriate concrete class derived from NodeInfo. The concrete implementation improves readability (since it qualifies the type of node information), and usually provides additional fields specific to the node type. ElementNodeInfoThis class is a concrete implementation of NodeInfo, and adds a local namespace field, as elements can have local namespaces: This class is a concrete implementation of NodeInfo, and adds a value field, as attributes have values: This class derives from AttributeNodeInfo. A processing instruction has a name and a value, like an attribute, but I've implemented a separate class to represent the concept of a processing instruction, even though it does not extend the AttributeNodeInfo class. This is merely a code readability decision. Instead of talking about the XmlTextReader as a class and its methods, which you can easily read about yourself, I'm going to show you the XmlTextReaderwithin the context of my Reader wrapper. This way, instead of just looking at documentation, you'll see the XmlTextReader in actual code, and I'll explain what I'm doing in the code and why. Creating an XmlTextReaderQuite literally, the first stumbling block is in creating an XmlTextReader. It sounds simple, but according to Microsoft:
Second, I want to control some aspects of the reading process, specifically, I almost always want to ignore whitespace. The default XmlTextReader returns all whitespace. So, to properly construct an XmlTextReader using Microsoft's recommended method and to have the ability to set some options, we have to do something like this: Before I go further, this constructor is the one I use for the unit tests, and it takes an XML string. You might instead want a constructor that takes a stream, and as you can see in the first line, I create a StringReader stream. The second line creates an XmlReaderSettings instance, and I explicitly (just to show you another useful property) choose not to ignore comments, but I do want to ignore whitespace. Next, I create the XmlTextReader from the stream. But that's not enough. I now have to create an XmlReader, passing in theXmlTextReader and the desired settings. Now, we have properly constructed a reader, complying with Microsoft's guidelines, and having the ability to configure the reader to ignore whitespace. If you're wondering about the last line, we'll get to that later. The Constructor Unit TestThe constructor reveals the fact that the XmlTextReader does not position itself on a valid node immediately after construction, as the NodeType is "None". Reading the XML DeclarationReading the XML declaration, as with all other elements, requires calling ReadNode: My wrapper for the reader optionally skips end elements. If you don't do this, the reader will return EndElement node types, which, depending on what you are doing with the XML, may be superfluous. In the unit test constructor, this flag is set to true. The XML Declaration Unit TestAn XML declaration contains attributes just like an element node. I'll demonstrate the ReadFirstAttribute and ReadNextAttribute shortly. Reading the Root Node and Other ElementsImmediately following the XML declaration should be the root node. My reader provides an Element property which returns an ElementNodeInfo instance that encapsulates the element name, prefix, and optional namespace. Looking at the implementation: You'll see that the ElementNodeInfo also consists of the reader's line and character position, and the element name is stripped of the prefix. Reading the Root Node Unit TestReading an element node is straightforward, as the unit test demonstrates: The ReadNode method is called to move past the XML declaration node and onto the root node. The unit test verifies that this happened correctly. There's another element test later on, which tests that a local namespace has been correctly read: Most XML elements contain attributes, and the root node includes two attributes, one of which is an XML namespace declaration. The XmlTextReader provides two methods for reading an attribute, MoveToFirstAttribute and MoveToNextAttribute, which return a boolean true if successful, false otherwise. I've modified this implementation slightly: and: Both of these methods return an AttributeNodeInfo instance, encapsulating the reader's line and character position and the attribute name, prefix, and value. A null is returned if there are no further attributes to read. You can use these methods, or you can use another method that avoids having to figure out whether to call ReadFirstAttribute or ReadNextAttribute. My reader figures this out automatically for you, and here's where the firstAttributeboolean comes into play: The firstAttribute flag is set whenever ReadNode is called. It's cleared when the first attribute is read, either by calling ReadFirstAttribute orReadAttribute. The Attribute Unit TestsThe following sequence of unit tests test the first, next, and "smart" attribute reader: A CDATA block lets you include freeform text in the XML, such as code. My reader provides a CData property which returns the CDATA text as a string: As you can see, the CData property validates the node type that wraps the Value property. The CDATA Unit TestReading XML comments is just like getting the CDATA text. Once we know that the node is a comment node, we return the Value property which contains the comment text. The reader also trims any leading and trailing whitespace, which is often used to make the XML comments more readable. As the XmlTextReader moves through the XML, any inner text is its own Text node type. The reader's Text property is a thin wrapper for theXmlTextReader's Value property: Process instructions are another kind of XML nodes. These may contain useful meta-instructions for the engine that is processing the XML. The reader provides a thin wrapper for getting the process instruction: Lastly, one of the important things about XML is that it is hierarchical. The reader provides a thin wrapper to the XmlTextReader's Depth property (a very thin wrapper): The point being though that we need this property implemented by any class that realizes IReader. The Depth Unit TestThis unit test reveals one of the side-effects of ignoring the XML end element node type, which is that the depth can pop several levels. This should be taken into consideration when writing an application that actually does something with the XML. About the Author
Introduction When my company asked me to work on this article for the client, I got a chance to work with JAXB concepts. I wanted to share my knowledge with you guys so that this will be helpful to me and people who are working on Java XML concepts. OvervieweXtensible Markup Language (XML) is a platform and language-independent way of defining tags. XML allows you to use meaningful tags to represent data in a structured format. Unlike HyperText Markup Language (HTML), which is used to display text, XML is a language to create extensible custom tags to represent the structure of data. SAX parser is an event driven low-level parser, that responds to the elements of an XML document as it parses through the documents. The data is not maintained in memory. Efficiency of parsing data into elements is the key feature of SAX parser. DOM parser is a high-level parser that maintains the data structures of a document in memory as it parses through the document. This helps you to manage and manipulate data as needed. DOM API follows the tree model for maintaining the data in memory. Formerly known as Project Adelard, Java Architecture for XML binding (JAXB) combines the benefits of Document Object Model (DOM) parser and Simple API for XML (SAX) parser. JAXB reduces the execution time significantly and creates a robust object model for enterprise level applications. This reference point explains how to use JAXB to create and distribute high-performance XML-enabled applications with minimum effort. Overview of XMLThis table describes the important components used in XML:
XML parsers are used to check whether XML documents are well formed or not. In addition, they are used to convert XML nodes into Java objects or elements. There are two types of parsers: validating and non-validating. Validating parsers verify that the XML document conforms to a DTD or a schema. A Java application that uses XML technologies needs to parse the data objects of XML. The parsing is done based on the schemas and DTDs that are defined for that particular document. You can use various technologies to parse an XML document into Java objects. Some of which are SAX, DOM and JAXB. Introducing JAXBYou can use JAXB API to map XML documents to Java objects. You can use JAXB API and tools to marshal or convert XML contents to Java objects. You can access and validate the Java objects against a schema. In addition, you can use JAXB API to un-marshal or convert Java objects into an XML document. Advantages of JAXBJAXB provides a layer of abstraction between XML documents and Java applications. This helps you develop enterprise applications. JAXB applications can match the speed of SAX by using the binding mechanism, which maintains information in the memory. In addition, JAXB can convert data in XML elements into equivalent valid Java objects and set constraints with the help of XML schemas or DTDs. JAXB frees you from writing and debugging the XML parsing codes. With generated conversion codes, you can write the applications that can access the XML documents through normal Java data interfaces. You can extend the functionalities for the generated class codes. In Java applications, you may want to have control over the XML schema components through the Java objects. For this, you can customize the binding schema for the different needs. Because of the object orientation, JAXB based applications are helpful to access multiple XML documents. Comparison of JAXB and SAXThe difference between JAXB and SAX are:
Although both JAXB and DOM have data storage capabilities, JAXB applications are specific to a single schema, whereas DOM applications can be managed with multiple schemas. JAXB applications are faster than DOM applications. Note:Since JAXB applications are specific to a single schema, they can utilize memory more efficiently in comparison to DOM applications. Architecture of JAXBThe basic components required for XML data binding are:
Complex data contents defined by using DTD or Schema in an XML document are bound to the content interfaces, which are generated by a JAXB compiler. Simple contents such as attributes and elements of an XML node are directly bound to the properties of the content interfaces and can be accessed through setter and getter methods. Using properties, you can reference two interfaces at the same time. You can effectively use the generated classes as both content interfaces and implementation interfaces to extend the functionality, which makes JAXB architecture more flexible and competent. Binding Schema/DTD to Java classesBinding Framework API helps you to compile XML Schema and Java classes for marshalling and un-marshalling of Java objects. Binding Derived ClassesWhen you bind DTD with a binding schema:
You can use:
If you do not mention the attribute types for components in the binding schema, the schema compiler uses the default String data type. Validations on XML Schema and Binding ObjectsYou have to validate a binding object against three constraints:
The Java programming language ensures that a schema constraint is checked at compile time and dynamically generates the available methods for the attribute mentioned in the schema. For example, if you have a schema constraint attribute by the name String, the derived methods will be: The enforcement performs a run-time check and throws an exception on failure. This is best suited for type constraints that do not map directly to the Java classes or primitives. For example, if an attribute is constrained to an integer value between 0 and 100, the set method can possibly throw a run-time exception when the value passed to the method is out of range. Complex Dynamic Schema Constraint EnforcementThe enforcement performs a check at run-time and throws an exception on failure.
The steps that you need to follow before working on JAXB generated classes are:
About the Author
Introduction License The Extensible Markup Language (XML) is a general-purpose markup language. It is classified as an extensible language because it allows its users to define their own tags. In Software Engineering, extensible refers to the system that can be modified by changing or adding features. Its primary purpose is to facilitate the sharing of data across different information systems, particularly via the Internet. XML is recommended by the World Wide Web Consortium (W3C). It is a fee-free open standard. The W3C recommendation specifies both the lexical grammar, and the requirements for parsing. The basic difference between HTML and XML is:
A well formed XML must have proper opening and closing tags. Data can be stored in child elements or in attributes, e.g.: Attributes are handy in HTML, but in XML, it is better to avoid them. Use child elements if the information feels like data. XML ValidationThere are two levels of correctness of an XML document:
A Well Formed XML document is a document that conforms to the XML syntax rules like:
A Valid XML document is a "Well Formed" XML document, which conforms to the rules of a Document Type Definition (DTD). Document Type Definition (DTD)The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. A DTD can be declared inline inside an XML document, or as an external reference. If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition with the following syntax: The DTD above is interpreted like this:
If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition with the following syntax: And this is the file note.dtd which contains the DTD: XML Schema is used to define the legal building blocks of an XML document, just like a DTD. XML Schemas are the successors of DTDs and also referred to as XML Schema Definition (XSD). XML Schemas are now used in most Web applications as a replacement for DTDs and in the near future, they will completely replace DTDs due to the following reasons:
An example of a very simple XML Schema Definition to describe a country is given below: An example of an XML document that confirms to this schema is given below (assuming the schema file name is country.xsdand both files are in the same directory): The best way to explain XQuery is to say that: "XQuery is to XML what SQL is to database tables" XQuery was designed to query XML data. XQuery is also known as XML Query. The mission of the XML Query project is to provide flexible query facilities to extract data from real and virtual documents on the World Wide Web, therefore finally providing the needed interaction between the Web world and the database world. Ultimately, collections of XML files will be accessed like databases. XQuery uses XPath (XPath is a language for finding information in an XML document. XPath is used to navigate through elements and attributes in an XML document.) expression syntax to address specific parts of an XML document. It supplements this with a SQL-like "FLWOR expression" for performing joins. A FLWOR expression is constructed from the five clauses after which it is named: FOR, LET, WHERE, ORDER BY, RETURN. ExampleLet's take any XML document: A simple XQuery can be written to extract a record out of this XML document like: The XQuery above will extract the following: XML document can be validated against XML schema (XSD). XSD checks the XML document's complete structure and reports an error if any datatype mismatches or node element does not exist. Below is sample code written in C# which takes an XML document and an XSD document as input and validates the XML document: This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Contents License
Let's first see a simple example: Usually, JavaScript code starts with the tag <script language="JavaScript"> and ends with the tag </script>. The code placed between <head> and </head>. Sometimes, people embed the code in the <body> tags: Why do we place JavaScript code inside comment fields <!-- and //-->? It's for ensuring that the Script is not displayed by old browsers that do not support JavaScript. This is optional, but considered good practice. The LANGUAGE attribute also is optional, but recommended. You may specify a particular version of JavaScript: You can use another attribute SRC to include an external file containing JavaScript code: For example, shown below is the code of the external file hello.js: The external file is simply a text file containing JavaScript code with the file name extension ".js". Note:
In order to output text in JavaScript you must use write() or writeln(). Here's an example: Note: the document object write is in lowercase as JavaScript is case sensitive. The difference between write andwriteln is: write just outputs a text, writeln outputs the text and a line break. Document objectThe document object is one of the most important objects of JavaScript. Shown below is a very simple JavaScript code: In this code, document is the object. write is the method of this object. Let's have a look at some of the other methods that the document object possesses. lastModifiedYou can always include the last update date on your page by using the following code: All you need to do here is use the lastModified property of the document. Notice that we used + to put together This page created by John N. Last update: and document.lastModified. bgColor and fgColorLets try playing around with bgColor and fgColor:
alertThere are three message boxes: alert, confirm, and prompt. Let's look at the first one: You can put whatever you want inside the quotation marks. confirmAn example for confirm box: promptPrompt box is used to allow a user to enter something according the promotion:
In all our examples above, we wrote the box methods as window.alert(). Actually, we could simply write the following instead as: Let's see an example: There are several concepts that we should know. First of all, var x= is a variable declaration. If you want to create a variable, you must declare the variable using the var statement. x will get the result, namely, true or false. Then we use a condition statement if else to give the script the ability to choose between two paths, depending on this result (condition for the following action). If the result is true (the user clicked "ok"), "Thank you" appears in the window box. If the result is false (the user clicked "cancel"), "Good choice" appears in the window box instead. So we can make more complex boxes using var, if and those basic methods. Another example: If you click "cancel", it will take you to yahoo, and clicking ok will continue with the loading of the current page "Welcome to my website!". Note:if (!x)means: if click "cancel". In JavaScript, the exclamation mark ! means: "none". FunctionFunctions are chunks of code.Let's create a simple function: Note that if only this were within your <script></script> tags, you will not see "Hello can you see me?" on your screen because functions are not executed by themselves until you call upon them. So we should do something: Last linetest() calls the function, now you will see the words "Hello can you see me?". Event handlerWhat are event handlers? They can be considered as triggers that execute JavaScript when something happens, such as click or move your mouse over a link, submit a form etc. onClickonClick handlers execute something only when users click on buttons, links, etc. Let's see an example:
The function ss() is invoked when the user clicks the button. Note: Event handlers are not added inside the <script>tags, but rather, inside the html tags. onLoadThe onload event handler is used to call the execution of JavaScript after loading:
onMouseover,onMouseoutThese handlers are used exclusively with links.
onUnloadonunload executes JavaScript while someone leaves the page. For example to thank users.
Handle multiple actionsHow do you have an event handler call multiple functions/statements? That's simple. You just need to embed the functions inside the event handler as usual, but separate each of them using a semicolon:
Let's say you have a form like this: Notice that we gave the names to the form and the element. So JavaScript can gain access to them. onBlurIf you want to get information from users and want to check each element (ie: user name, password, email) individually, and alert the user to correct the wrong input before moving on, you can use onBlur. Let's see how onblur works:
If you enter an email address without the @, you'll get an alert asking you to re-enter the data. What is:x.indexOf(@)==-1? This is a method that JavaScript can search every character within a string and look for what we want. If it finds it will return the position of the char within the string. If it doesn't, it will return -1. Therefore,x.indexOf("@")==-1 basically means: "if the string doesn't include @, then: What's focus()? This is a method of the text box, which basically forces the cursor to be at the specified text box.onsubmit Unlike onblur, onsubmit handler is inserted inside the <form> tag, and not inside any one element. Lets do an example: Note: Protect a file by using LoginLet's try an example
|| means "or", and ,!= indicates "not equal". So we can explain the script: "If the id does not equal 'Sam', or the password does not equal 'Sam123', then show an alert ('Invalid Login') and stop submitting. Else, open the page 'main.htm'". LinkIn most cases, a form can be repaced by a link: More examples: Let's see an example: To activate a Date Object, you can do this: var x=new Date(). Whenever you want to create an instance of the date object, use this important word: new followed by the object name(). Dynamically display different pagesYou can display different pages according to the different time. Here is an example:
Open a windowTo open a window, simply use the method "window.open()":
You can replace test.htm with any URL, for example, with http://www.yahoo.com. Size, toolbar, menubar, scrollbars, location, statusLet's add some of attributes to the above script to control the size of the window, and show: toolbar, scrollbars etc. The syntax to add attributes is:
For example: Another example with no attributes turned on, except the size changed: Here is the complete list of attributes you can add:
ReloadTo reload a window, use this method:
Close WindowYour can use one of the codes shown below:
LoadingThe basic syntax when loading new content into a window is:
This is the same as Let's provide an example, where a confirm box will allow users to choose between going to two places: Remote Control WindowLet's say you have opened a new window from the current window. After that, you will wonder how to make a control between the two windows. To do this, we need to first give a name to the window.Look at below:
By giving this window a name "aa", it will give you access to anything that's inside this window from other windows. Whenever we want to access anything that's inside this newly opened window, for example, to write to this window, we would do this: aa.document.write("This is a test."). Now, let's see an example of how to change the background color of another window: openerUsing "opener" property, we can access the main window from the newly opened window.
Let's create Main page: Then create Remote control page (in this example, that is test.htm): Try it now! FrameOne of the most popular uses of loading multiple frames is to load and change the content of more than one frame at once. Lets say we have a parent frame: We can add a link in the child frame "frame1" that will change the contents of not only page1, but page2 too. Shown below is the html code for it: Notice: You should use "parent.frameName.location" to access another frame. "parent" standards for the parent frame containing the frameset code. This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below. A list of licenses authors might use can be found here
Car insurance is a type of cover that can be quite costly depending on your needs and circumstances, but this is a type of cover that is a legal requirement for drivers in the UK so no matter what the expense you have to get this cover if you wish to take your vehicle on the road. It is important to remember that there are ways and means of lowering your insurance premiums on car cover, however, so with a little thought and research you could save yourself a small fortune.
|
Recent Posts
The XmlTextReader - A Beginner's Guide
Creating XML and Java Technology Based Application Source Code : XML for Beginners Source code : JavaScript For Beginners Top 5 ways to save on car insurance Top Posts
Custom Programming and Web Development
3 Reasons to Host Your Web Site in a Linux Server The best one, Unix v/s Windows? How to go selecting a cheap web hosting company? SQL Server Notification Services Removed SQL 2008 Recent Comments
The XmlTextReader - A Beginner's Guide
Source code : JavaScript For Beginners Source Code : XML for Beginners Top 5 ways to save on car insurance Why Is There A Playstation 3 Shortage? Categories Archive Syndication Tools |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
You are not logged in. FREE Sign Up or Log In
©2009 Flixya Entertainment, LLC. All rights reserved.






