Here are the top 10 good XML habits to adopt:The XPath hint:
- Define your XML and encoding
- Use a DTD or XSD
- Remember to validate
- Validation isn't always the answer
- XML structure versus attributes
- Use XPath to find information
- You don't always need a parser to extract information
- When to use SAX over DOM parsing
- When to use DOM over SAX parsing
- Use a good XML editor
When working with XML data, finding the information you want can be complex. You can, of course, write a parser to pick out the material that you need, but sometimes, you really just need to find a small fragment of the information in the file very quickly. For example, if you wanted to extract a list of all the countries in your contacts XML file so that you could see how widely spread your contacts were, you could use XPath to pick out the information. XPath enables you to pull out the data from an XML file by using the structure of the XML file as part of the query. You can, for example, extract the data for a specific element by giving the path to the element within the XML file:$ xpath contacts.xml '//contact/address/country'You can dissect the content like this:Any hints you'd offer in addition to these? One that I thought was missing was:
- The initial double slash (//) specifies to look anywhere within the document for the specified element (contact).
- The next slash and element name specify the next element to pick out (address)—that is, look for the 'ddress element within the contact element.
- The final one repeats the process, this time picking the country.Note that in the example, you qualified the type of address to select the information from, so it will pick all addresses. You can see the result of the XPath query.$ xpath contacts.xml '//contact/address/country' Found 3 nodes: -- NODE -- USA-- NODE -- USA-- NODE -- USAIf you want to pick out more specific data, you can specify the element contents, or attribute contents that you want to match. For example, to select only mobile phone numbers, you need to specify the attribute type and value. To do this, use an at sign (@), which specifies that you want to search an attribute, and then specify the value you want to match.$ xpath contacts.xml '//contact/phone[@type="mobile"]' Found 1 nodes: -- NODE -- 123 456 7890[These] use a command line tool. Many XML toolkits provide native methods to work with XPath elements, and you can extract data using the XPath specification to use in your applications directly, without having to work with a parser to get the information.
- Don't mix text data with child nodes - In other words, if you're going to add content as a child node to a given XML node, you shouldn't add more content as text, like this: baz Here's a text node, a sibling of the 'bar' node