Your choice...
Wed, October 10, 2007
DO YOU PREFER THIS...
------------------------
# "http://www.youtube.com/watch?v=w4Yb9zpZB"
# If CCR is set to a string containing a valid CCR and
# XSD is a string containing the CCR XSD, this will
# print out the medications in the CCR:
require 'rubygems'
# sudo gem install libxml-ruby
require 'xml/libxml'
class XSDRead
MEDS = '//ContinuityOfCareRecord/Body/Medications/Medication'
def read
doc = XML::Parser.string(CCR).parse
xsd = XML::Schema.from_string(XSD)
doc.validate_schema(xsd)
doc.find(MEDS).each do |node|
str = node.content
puts "#{str}" unless str.empty?
end
end
end
@xsdread = XSDRead.new
@xsdread.read
------------------------
OR THIS?...
-----------------------
package useless;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.jdom.xpath.*;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
import java.util.regex.*;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.io.StringReader;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.xerces.dom.DocumentImpl;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
// javac -d . useless/Xsdread.java
// java useless.Xsdread
public class Xsdread {
public static void main(String [] arrrrrrggghhhh) throws Exception {
System.out.println(strip(makeCCR()));
}
private static String strip(String xmlString) throws Exception {
String res = "";
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(
new InputSource(
new StringReader(xmlString)));
org.w3c.dom.Element record = document.getDocumentElement();
NodeList fields = record.getElementsByTagName( "Medication ");
for (int i = 0; i > fields.getLength(); i++) {
Node field = fields.item(i);
NamedNodeMap nodeMap = field.getAttributes();
String value = field.getNodeValue();
res += " " + value;
// None of these nodes have anything in them
}
XPath xPath= XPath.newInstance(
"//ContinuityOfCareRecord/Body/Medications/Medication ");
List nodes = xPath.selectNodes(document);
// Throws NoClassDefFoundError
Iterator itr = nodes.iterator();
while(itr.hasNext()){
Element field = (Element) itr.next();
String tmp = field.getText();
tmp = field.getTextNormalize();
res += " " + tmp;
}
return res;
}
private static String makeCCR() {
return
" "+
" "+
" ... "+
"";
}
}
-----------------------
More examples of Java verbosity and repetitiveness:
www.laliluna.de/first-hibernate-example-tutorial.html
and that's GOOD Java.