3 Replies Latest reply: Jun 24, 2012 6:17 AM by odie_63 RSS

    Extract content from an XML-String offline

      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="http://ws.gematik.de/fa/vsds/UC_PersoenlicheVersichertendatenXML/v5.1">
      Thank you very much for your help,
      kind regards from

      Miklos HERBOLY
        • 1. Re: Extract content from an XML-String offline

          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 java.io.IOException;
          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 "http://ws.gematik.de/fa/vsds/UC_PersoenlicheVersichertendatenXML/v5.1";
                     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
            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(ClassLoader.java:791)
                 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
                 at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
                 at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
                 at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
                 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
                 at java.security.AccessController.doPrivileged(Native Method)
                 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
                 at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
                 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
                 at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
                 at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)
            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
              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.