Posts

Showing posts with the label XML

Java XML Parsing Made Easy

Unlike with many other modern languages, parsing XML in Java requires more than one line of code. XML traversing using XPath takes even more code, and I find this is unfair and annoying. I'm a big fan of XML and use it it in almost every Java application. Some time ago, I decided to put all of that XML-to-DOM parsing code into a small library —jcabi-xml. Put simply, the library is a convenient wrapper for JDK-native DOM manipulations. That's why it is small and dependency-free. With the following example, you can see just how simple XML parsing can be: import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; XML xml = new XMLDocument(   "<root><a>hello</a><b>world!</b></root>" ); Now, we have an object of interface XML that can traverse the XML tree and convert it back to text. For example: // outputs "hello" System.out.println(xml.xpath("/root/a/text()").get(0)); // outputs the entire XML d...

Document Type Definition (DTD)

As should you know by now, XML by itself only serves to deliver data. You must provide other files to format and validate the XML code. This is where the DTD and schema come into play. The advancement of technology has provided many ways to validate XML. The DTD is just one. Document Type Definitions provide rules the computer can follow when processing the code. The DTD and schema are the precursors to XSL. Although still used in some formats, learning to write these files is not a requirement to create XML platforms, but it is indispensable to know what they are and how they function. These are terms you will stumble across in your studies frequently and understanding the basic design will be an advantage.What is a DTDThis is an explanation of document type definitions and the role they play in XML design. DTD code comes in two forms, internal and external. This article provides a description and explanation of how DTD files work.The DTD: Elements, Entities, Attributes, and Notation...

XML Schema (XSD)

When beginning a study of XML, some may encounter an unfamiliar term that leaves them asking the question, “What is an XML schema (XSD)?” An XML schema is a road map for the XML document similar to a Document Type Definition (DTD). Created by the World Wide Web Consortium (W3C), schemas describe the elements and map out the presentation and nesting of XML documents. Essentially, the schema enables all applications to understand the flow of the page and validate the elements. Why Use An XML Schema? Step away from websites for a minute and consider house building. If the plumber, electrician and contractor all do their own thing, the result is a building with an uneven line, sinks in the bedroom and cable hookups in the bathroom. That does not happen because the architect draws a diagram that maps out the basic structure. With a blueprint, there is no guesswork. The plumber understands where the sinks go, and the electrician knows what rooms need what type of wiring. A schema is the...

Difference between XML Namespace (xmlns) and XML Schema (XSD)?

XML namespaces are used for providing uniquely named elements and attributes in an XML document. XML Schema is an XML-based alternative to DTD.It describes the structure of an XML document. A schema describes the structure of the XML: tag names, parent/child relationships, cardinality, types, restrictions, etc. Namespaces are more like packages in Java: They give you a way to distinguish your tags from others. You can combine XML streams together. If you and someone else both have a <name> tag, you can have namespace foo, someone else can have namespace bar, and you can tell the difference with <foo:name> and <bar:name>.