Part 3: Allowing binding to missing XML nodes.
You might notice that if you try to bind to an element that does not exist in the model, you will get an error in the call to evaluate. This adds some complexity and burden so we will make a function that ensures the bound path exists in the XML DOM. Ensuring the xpath makes development much easier.
Trying to share any coding knowhow whenever I run into any issues, which is every day in the web programming world.
Saturday, December 31, 2011
Simplify with an XML data model - Part 3
Friday, December 30, 2011
Simplify with an XML data model - Part 2
Part 2: Setting the form values from XML (Bi-directional binding).
Binding should work two ways, updating the model from the form fields and updating the form fields from the model. Let’s update the code so the form is populated with data when the page is loaded.
Wednesday, December 28, 2011
Simplify with an XML data model - Part 1
Part 1: Binding from XML to the HTML form.
When doing data binding, you typically need to map a field in the UI with some part of the data model. XPath is a terrific solution to this. All you need is an XPath expression that evaluates to a single element or attribute node. This allows for bidirectional binding between HTML <form> elements and XML nodes. Binding to read-only HTML elements can be done with more complex XPath expressions and involve several nodes and functions, e.g. concatenating several fields together. It might make sense to allow for one input to map to many nodes in the future.
Interactive Example:
Interactive Example:
Monday, December 26, 2011
Simplify with an XML data model - Introduction
A system for XML data binding in the browser with XPath and client side validation using Schematron and XSLT.
Introduction
As a Java developer I find myself wondering why I spend so much time dealing with data models. For each application I work on, my time is mostly spent creating a data model in the database, a corresponding data model business object in Java code, and form value objects to collect data from web pages. Then I have to write the code that copies all the fields between models. Then there is the problem of validation rules, which require yet another framework.
After working with web services and JAXB, I started to wonder if there was an easier and more flexible way of representing all of this data in XML and skipping all the myriad Java data model classes. It turns out that XML makes for a great data model. It can represent any type of data, is easy to write, access, transform and exchange. It’s the X in AJAX for crying out loud!
Tuesday, March 22, 2011
Dealing with cyclic data in JAXB
JAXB is a great tool for converting Java data into XML. It does have some shortcomings, one of these is cyclic references. XML is great for tree structures, but any cycles will cause problems as your XML would end up infinitely nesting. There are some tricks explained here, but I was still having trouble marshaling my node data. I ended up needing to use a combination of XmlID + XmlIDREF, XmlTransient, and a wrapping object that transforms the cyclic tree into a list of all nodes in the tree and a reference to the root node.
Saturday, March 19, 2011
Selecting a Random Row With MySQL + PHP
You’d think it was as easy as pie to do, but there is no simple way to obtain a random row that is efficient at the same time. If you Google for “mysql random row” you might have seen this:
This looks easy, but if you do an EXPLAIN on that query, you will notice that the entire table is scanned for just one row.
That means the bigger your table is, the longer it will take to execute.
One easy way to get around this, is to split the logic into three parts.
SELECT * FROM `myTable` ORDER BY RAND() LIMIT 1
This looks easy, but if you do an EXPLAIN on that query, you will notice that the entire table is scanned for just one row.
That means the bigger your table is, the longer it will take to execute.
One easy way to get around this, is to split the logic into three parts.
- Get a count of all the rows in your table
- Find a random number between 0 and the count
- Do your query with a LIMIT clause based on that random number
$countQuery = "SELECT count(*) count FROM `myTable`"; $countQueryResult = mysql_query($countQuery); $count = mysql_result($countQueryResult, 0, "count"); $randRow = rand(0, $count-1); $randomRowQuery = "SELECT * FROM `myTable` LIMIT $randRow, 1";By doing this, even though you are doing two selects instead of one, the search time will become constant instead of linear and you will same precious time when your tables become larger.
Labels:
php mysql sql
Subscribe to:
Posts (Atom)