Displaying keyword search results 1 - 10
Created by alfa on June 03, 2011 09:41:03 Last update: June 03, 2011 09:41:03
Dynamic proxy can be used to eliminate the need to stub out unused interface methods. This is an example for a simple SAX content handler for XML parsing. The org.xml.sax.ContentHandler interface requires 11 methods be implemented but we only need three:
import java.io.IOException; import java.util.Ar...With a dynamic proxy, we don't need the empty blocks for the unused methods:
import java.io.IOException; import java.util.Ar...Equivalently (with anonymous inner class):
import java.io.IOException; import java.util.Ar...demo.xml :
<breakfast-menu> <food> <name>Belgian W...
Created by freyo on May 23, 2011 14:30:18 Last update: May 23, 2011 14:31:08
There are two distinct ways to process XPath: with namespace and without namespace. The code is different depending on whether the parser is namespace aware. Code without namespace:
import java.io.*; import javax.xml.parsers.*; ...code with namespace:
import java.io.*; import java.util.Iterator; ...XML without namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...XML with namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...The same XPath expression works for both XML files when the parser is not namespace aware. When the parser is namespace aware, you have to adjust the XPath accordingly depending on whether the XML has namespace declarations: " /test-license/licensee/name/text() " works for the XML file without namespace, while " /p:test-license/p:licensee/p:name/text() " works for the XML file with namespace.
Created by nogeek on February 08, 2011 13:46:18 Last update: February 08, 2011 13:46:18
Simple Java code that does XSLT on an XML file. The transform results go to STDOUT.
import java.io.*; import javax.xml.parsers....
Created by nogeek on November 04, 2010 20:00:15 Last update: November 05, 2010 14:38:43
Following are some bugs in the Xalan jar shipped with JBoss 5.1.0 GA and JBoss 6.0. The Xalan jar file is located in jboss-5.1.0.GA/lib/endorsed ( %JBOSS_HOME%/common/lib for JBoss 6.0). Test xml:
<?xml version="1.0" encoding="ISO-8859-1"?> ...Test xsl:
<?xml version="1.0" encoding="ISO-8859-1"?> <xs...XSLT Java code:
import org.w3c.dom.*; import javax.xml.parsers....DateUtil.java
import java.util.Date; public class DateUti...XSLT output:
Transformer Factory class: class org.apache.xalan....Apparently, the output is wrong. The string "A test event" should not have been displayed.
Created by Dr. Xi on August 27, 2010 22:35:53 Last update: August 27, 2010 22:35:53
This example decompresses a gzip compressed file with java.util.zip.GZIPInputStream . The input is assumed to be Base64 encoded after being gzipped (such would be the case when binary data is transmitted within an XML file). You don't need the Base64InputStream if the data is not Base64 encoded.
import java.io.*; import java.util.zip.GZIPInpu...
Created by Dr. Xi on July 19, 2010 21:58:34 Last update: July 23, 2010 21:37:23
Parsing XML in Java is really simple:
import java.io.*; import javax.xml.parsers.Docu...The parser implementation details are hidden behind the JAXP API. In case you want to know which parser implementation is used, this is what the JavaDoc for DocumentBuilderFactory.newInstance says: Use the javax.xml.parsers.DocumentBuilderFactory system property. Use the properties file " lib/jaxp.properties " in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to...
Created by Dr. Xi on July 21, 2010 22:14:53 Last update: July 21, 2010 22:14:53
This is a Java program to test XPATH expressions with namespace option. It's been tested with JDK1.6.
import java.io.FileInputStream; import java.uti...Following Getting Started with XML Security , these are the test files: Without namespace (patient.xml).
<PatientRecord> <Name>John Doe</Nam...With namespace (patient_ns.xml).
<med:PatientRecord xmlns:med="http://www.medic...Test results:
C:\>java XPathExample patient.xml /PatientRecord/V...
Created by Dr. Xi on June 20, 2010 14:35:17 Last update: June 20, 2010 14:35:17
This XML signature validator comes from the Apache XML Security project. It validates the signature according to the core validation processing rules . It does not verify that the key used to generate the signature is a trusted key. You can override the KeySelector class to make sure that the signing key is from a trusted store.
import javax.xml.crypto.*; import javax.xml.cry...
Created by Dr. Xi on June 19, 2010 04:34:01 Last update: June 19, 2010 04:39:13
Java SE 6 contains built-in utilities to generate XML signatures. This is an example that generates XML signatures using a Java keystore. It has options to generate signature for the whole document, for an element with a specific ID, or for elements matched by an XPATH expression. The XML document used to test is taken from Getting Started with XML Security :
<?xml version="1.0"?> <PatientRecord> ...This is the Java code:
import java.io.FileInputStream; import java.io....However, it looks like the XPATH transform is not working. The digest generated with XPATH filter is exactly the same as that without it (i.e., the whole document)! Another reference: Programming With the Java XML Digital Signature API
Created by Dr. Xi on June 11, 2010 23:11:59 Last update: June 11, 2010 23:14:02
Given a simple XML file like this:
<?xml version="1.0"?> <root id="1"> ...Calling Document.getElementById returns null (surprisingly!):
import java.io.*; import org.w3c.dom.*; impo...In fact the JavaDoc says something along the lines that getElementById returns the Element that has an ID attribute with the given value. An attribute with the name "ID" or "id" is not of type ID unless it is so defined. How is an attribute defined as an ID attribute ? With a DTD or schema. If you are not validating the XML, then the API is useless. So, what to do if you want to find an element for which the attribute named "id" has a given value? Several options were offered in GetElementById Pitfalls . One of them is to...