This discussion is archived
3 Replies Latest reply: Jun 24, 2012 4:17 AM by odie_63 RSS

Extract content from an XML-String offline

mh*379860*ly Newbie
Currently Being Moderated
HI Gentlemen,

Could you help me in solving this problem? I have got a Java String (see below) read in from a German eHealth smartcard. I must extract the tag values and put them into local variables for further processing in my Java module. How to proceed without connecting to a database, simply to analyze the source--the String variable? Note that lines are separated by 0x0A only.
<?xml version="1.0" encoding="ISO-8859-15" standalone="yes"?>
<UC_PersoenlicheVersichertendatenXML CDM_VERSION="5.1.0" xmlns="">
Thank you very much for your help,
kind regards from

  • 1. Re: Extract content from an XML-String offline
    odie_63 Guru
    Currently Being Moderated

    Why would you need a database for that?

    Use a Java DOM, SAX or XPath processing API, you'll find lots of examples on the Web.

    For instance, using the XPath API of Java 1.5 (and up) :
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;
    import javax.xml.XMLConstants;
    import javax.xml.parsers.*;
    import javax.xml.xpath.*;
    import javax.xml.namespace.NamespaceContext;
    import java.util.Iterator;
    public class XPathSampleClass {
         private static class XPathNSContext implements NamespaceContext {
             public String getNamespaceURI(String prefix) {
               if (prefix.equals("ns")) {
                 return "";
               return XMLConstants.NULL_NS_URI;
             public String getPrefix(String uri) { return null; }
             public Iterator getPrefixes(String uri) { return null; }
        public static void main(String[] args) 
          throws ParserConfigurationException, SAXException, IOException, XPathExpressionException 
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = dbf.newDocumentBuilder();
          Document doc = builder.parse("c:\\dev\\workspace\\otn\\smartcard.xml");
          XPath xp = XPathFactory.newInstance().newXPath();
          xp.setNamespaceContext(new XPathNSContext());
          String v_Versicherten_ID = (String) xp.evaluate("/ns:UC_PersoenlicheVersichertendatenXML/ns:Versicherter/ns:Versicherten_ID", doc, XPathConstants.STRING);
          String v_Geburtsdatum = (String) xp.evaluate("/ns:UC_PersoenlicheVersichertendatenXML/ns:Versicherter/ns:Person/ns:Geburtsdatum", doc, XPathConstants.STRING);
          String v_Strasse = (String) xp.evaluate("/ns:UC_PersoenlicheVersichertendatenXML/ns:Versicherter/ns:Person/ns:StrassenAdresse/ns:Strasse", doc, XPathConstants.STRING);
          //System.out.println("Versicherten_ID = " + v_Versicherten_ID);
          //System.out.println("Geburtsdatum = " + v_Geburtsdatum);
          //System.out.println("Strasse = " + v_Strasse);
    (I used an external XML file here for convenience, but of course you can also build the DOM document out of your existing String instance)

    Edited by: odie_63 on 23 juin 2012 19:06
  • 2. Re: Extract content from an XML-String offline
    mh*379860*ly Newbie
    Currently Being Moderated
    HI odie_63,

    Thank you for your reply. This is somewhat more complicated that I would have estimated--so your help is again very highly appreciated. I tried the code with the xml file (just to see if the java portion works) and became a funny error message where I do not know where I am false. Please drop a few lines of your opinion.

    Here is my classpath:
    and the output of JVM:
    java.lang.NoClassDefFoundError: XpathSampleClass (wrong name: XPathSampleClass)
         at java.lang.ClassLoader.defineClass1(Native Method)
         at java.lang.ClassLoader.defineClass(
         at Method)
         at java.lang.ClassLoader.loadClass(
         at sun.misc.Launcher$AppClassLoader.loadClass(
         at java.lang.ClassLoader.loadClass(
         at sun.launcher.LauncherHelper.checkAndLoadMain(
    Exception in thread "main" 
    As seen, the classpath has a dot (.) for the current directory c:\ocf where all the stuff is located. Neither could I find any mistypes as of class names.

    Thank you, kind regards

  • 3. Re: Extract content from an XML-String offline
    odie_63 Guru
    Currently Being Moderated
    java.lang.NoClassDefFoundError: XpathSampleClass (wrong name: XPathSampleClass)
    If you're using my sample code verbatim, then the class is named "XPathSampleClass" (capital P), and of course the .class file must have the same name.


  • Correct Answers - 10 points
  • Helpful Answers - 5 points