background preloader

XPath Syntax

XPath Syntax

IAmDann Ruby/XML, XSLT and XPath Tutorial What is XML ? The Extensible Markup Language (XML) is a markup language much like HTML or SGML. This is recommended by the World Wide Web Consortium and available as an open standard. XML is a portable, open source language that allows programmers to develop applications that can be read by other applications, regardless of operating system and/or developmental language. XML is extremely useful for keeping track of small to medium amounts of data without requiring a SQL-based backbone. XML Parser Architectures and APIs: There are two different flavors available for XML parsers: SAX-like (Stream interfaces) : Here you register callbacks for events of interest and then let the parser proceed through the document. SAX obviously can't process information as fast as DOM can when working with large files. SAX is read-only, while DOM allows changes to the XML file. Parsing and Creating XML using Ruby: The most common way to manipulate XML is with the REXML library by Sean Russell. DOM-like Parsing:

Vimcasts - free screencasts about the text editor Vim Nicholas' Adventures: Nokogiri - Cut With Precision Many times we as developers have to deal with complex data, be it an ActiveResource result set or a HTML/XML document. Trying to parse data out of these using for each and nesting loops within loops can be cumbersome. A more elegant solution is to use nokogiri and xpath. Nokogiri is a type of Japanese saw, it also is a gem in Ruby that you can use to easily deal with XML or HTML documents. $ sudo gem install nokogiri Now consider the following XML document: foods.xml Before we can work with our data we need to read XML into Nokogiri. > require 'rubygems' > require 'nokogiri' > doc = Nokogiri::XML.parse(File.read('foods.xml')) => #<Nokogiri::XML::Document:0x3f930c9db884 ... What we are returned is a Nokogiri document which is a collection of Nokogiri elements and text objects. So for example if we wanted to know all the names of the food items in our document we simply say: > doc.xpath("//name").collect(&:text) => ["carrot", "tomato", "corn", "grapes", "orange", "pear", "apple"] Learn More

Java Anti-Patterns This page collects some bad code that may not look so obviously bad to beginners. Beginners often struggle with the language syntax. They also have little knowledge about the standard JDK class library and how to make the best use of it. Some of these may seem like micro-optimization, premature optimization without profiling or constant factor optimizations. If you are interested in how to pogram compiler friendly, look at the JDK Performance Wiki. In the end a lot of your application's performance depends on the overall quality of your code. Compare these scenarios (assume 100MB young generation): In the slower scenario the transaction duration is 10 times longer. String concatenation String s = ""; for (Person p : persons) { s += ", " + p.getName(); } s = s.substring(2); //remove first comma This is a real memory waster. Lost StringBuffer performance StringBuffer sb = new StringBuffer(); sb.append("Name: "); sb.append(name + '\n'); sb.append("!") String s = "Name: " + name + "\n!"

Nokogiri From a String We’ve tried to make this easy on you. Really! We’re here to make your life easier. The variables html_doc and xml_doc are Nokogiri documents, which have all kinds of interesting properties and methods that you can read about here. We’ll cover the interesting bits in other chapters. From a File Note that you don’t need to read the file into a string variable. Clever Nokogiri! From the Internets I understand that there may be some HTML documents available on the World Wide Web. Parse Options Nokogiri offers quite a few options that affect how a document is parsed. NOBLANKS - Remove blank nodesNOENT - Substitute entitiesNOERROR - Suppress error reportsSTRICT - Strict parsing; raise an error when parsing malformed documentsNONET - Prevent any network connections during parsing. Here’s how they are used: Or Encoding Strings are always stored as UTF-8 internally. Some documents declare one particular encoding, but use a different one. Remember that data is just a stream of bytes.

RESTful web services with MongoDB A few tutorials ago,in the tutorial about RestEasy and Hibernate, we saw how we could integrate RESTEasy with Hibernate.Now in this tutorial we will see how can we create Restful web services with RestEasy and MongoDB. As an example we will see how to expose the documents of collection named Categories.The service class will be: Since our service class is ready we now need to create the Application class: package com.javaonly.service; import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; public class CategoryApplication extends Application { private Set<Class<?>> classes=new HashSet<Class<? Finally, in order to configure RESTEasy we must add the following snippet in the web.xml: Now every DAO class will extend AbstractDAO class.So in the case of categoryDAO we will have: package com.javaonly.mongo.daos; public class CategoryDao extends AbstractDao{ public CategoryDao(){ super(); } } As can see in the above class we've used a class named MongoDBUtil.

Getting Started with Nokogiri and XML in Ruby | 58bits Here's a short post on getting started with Nokogiri - a Ruby gem that wraps libxml. I'm writing this because well, the docs at Nokogiri kind of suck. I wanted to read a simple XML document. My XPath fu was a little rusty, although all I wanted to do was read some attributes from a root element, some element values off of the root, and then a short collection of items (very similar to an Atom document). My main bone of contention with the Nokogiri docs was their use of the @doc.xpath("//character") search operator at the very beginning of their parsing tutorial. How about we start from the beginning: Here is a sample XML document. <Collection version="2.0" id="74j5hc4je3b9"><Name>A Funfair in Bangkok</Name><PermaLink>Funfair in Bangkok</PermaLink><PermaLinkIsName>True</PermaLinkIsName><Description>A small funfair near On Nut in Bangkok. From our IRB prompt - the first thing we'll do is require nokogiri. >> require 'nokogiri' => true Now let's load our XML document. or >> f.close

Binary Trees by Nick Parlante This article introduces the basic concepts of binary trees, and then works through a series of practice problems with solution code in C/C++ and Java. Binary trees have an elegant recursive pointer structure, so they are a good way to learn recursive pointer algorithms. Contents Section 1. Stanford CS Education Library -- #110 This is article #110 in the Stanford CS Education Library. Related CSLibrary Articles Linked List Problems ( -- a large collection of linked list problems using various pointer techniques (while this binary tree article concentrates on recursion) Pointer and Memory ( -- basic concepts of pointers and memory The Great Tree-List Problem ( -- a great pointer recursion problem that uses both trees and lists Section 1 -- Introduction To Binary Trees Binary Search Tree Niche Basically, binary search trees are fast at insert and lookup. Strategy Lookup() 1.

Tree List Recursion Abstract Stanford CS Education Library: one of the neatest recursive pointer problems ever devised. This is an advanced recursive pointer problem that uses a binary tree and a doubly linked list. You should be comfortable with pointers and recursion to understand this problem. This article introduces the problem with a few memory diagrams, gives some hints, and provides solution code in both Java and C/C++. See: TreeListRecursion.html Or as a PDF: TreeListRecursion.pdf -- the same content, but in PDF, so both the text and images are in the one file See also... Linked Lis tBasics -- introduction to linked lists Linked List Problems -- more advanced linked list problems Binary Trees -- introduction to binary trees Downloading help Up to the CS Education Library Home Tricks with Direct Memory Access in Java « Highly Scalable Blog Java was initially designed as a safe, managed environment. Nevertheless, Java HotSpot VM contains a “backdoor” that provides a number of low-level operations to manipulate memory and threads directly. This backdoor – sun.misc.Unsafe – is widely used by JDK itself in the packages like java.nio or java.util.concurrent. Obtaining Unsafe The sun.misc.Unsafe class is so unsafe that JDK developers added special checks to restrict access to it. Fortunately there is theUnsafe field that can be used to retrieve Unsafe instance. In the next sections we will study several tricks that become possible due to the following methods of Unsafe: sizeof() Function The first trick we will do is C-like sizeof() function, i.e. function that returns shallow object size in bytes. As so, we can implement sizeof() as follows: We need to use normalize() function because addresses between 2^31 and 2^32 will be automatically converted to negative integers, i.e. stored in complement form. Direct Memory Management

Don't write on the whiteboard :: Joseph Perla I recently interviewed at a major technology company. I won't mention the name because, honestly, I can't remember whether I signed an NDA, much less how strong it was. I did well. Mostly because of luck. 1. When I interviewed at Palantir around 5 years ago, I had a lot of trouble with this. Most people think you have to write on the whiteboard. The interviewer started by asking me to code up a simple recursive calculation, using any language I wanted. The interviewers don't care. 2. So I asked for some paper and a pen. You should always have paper and pen anyway to write down ideas. Some of the best programmers figure out the high-level overview on paper before they write a single line of new code. 3. Even if you are a C++ systems guru. You will waste a lot of time writing string manipulation code and initialization code that you can do in one line of Python. All I had was a post-it note, a tiny amount of space. 4. Five minutes later, I had my algorithm. 5. 6. Don't just read blogs. 7.

Related: