Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Trouble XML parsing

Help
2006-09-17
2013-05-08
  • as I asked on the groovy list here is the basic XML parsing script that does not work.

    /*
    * Menu: HibAnalyzer
    * Script-Path: /GroovyMonkeyScripts/monkey/HibernateAnalyzer.gm
    * Kudos: alex
    * License: EPL 1.0
    * Exec-Mode: Foreground
    * DOM: http://groovy-monkey.sourceforge.net/update/plugins/net.sf.groovyMonkey.dom
    */
    import groovy.util.*

    monitor.beginTask( '', 1)
    if( monitor.isCanceled() ) return

    def s = """
    <hibernate-mapping>\n
      <class name="test">\n
         <property name="txt" type="java.lang.String">\n
            <column name="TXT" sql-type="VARCHAR2(50)" not-null="true"/>\n
         </property>\n
      </class>\n
    </hibernate-mapping>\n
       """

    def slurper = new XmlSlurper(false,false)
    try {
       def mapping = slurper.parse(s.toString())
    } catch (Exception ex) {
       println ex.getMessage()
       System.out.println ("hello")
    }

    monitor.worked( 1 )

    monitor.done()

    I get a exception from Groovy: java.net.MalformedURLException

    I am not sure how to get the stack trace, since

    exception.printStackTrace(System.out)

    does not print anytihng in the console.

    I modified the script and it does not require any outside resources, since XML specified in the script itself.

    If I run the script as standalone groovy script it works with JSR-06 version.

    Any ideas?

     
    • James E. Ervin
      James E. Ervin
      2006-09-17

      Wow I cut and pasted your script and got the following:

      no protocol: 
      <hibernate-mapping>

      <class name="test">

      <property name="txt" type="java.lang.String">

      <column name="TXT" sql-type="VARCHAR2(50)" not-null="true"/>

      </property>

      </class>

      </hibernate-mapping>

      This is what you were expecting right?
      James

       
    • No, I was expecting the script to actually parse the XML and yes that is what I get.

      That is the message of the exception. The exception itself is java.net.MalformedURLException

      This is really strange. I will read up a bit more on how slurper does it, maybe tomorrow.

      thanks,
      alex.

       
    • James E. Ervin
      James E. Ervin
      2006-09-17

      I found the trouble, I rewrote your script using the out DOM and the apache commons lang ExceptionUtils class.  The problem is that you were calling slurper.parse() which expects a string that is a URI, you should call parseText().

      <pre>
      --- Came wiffling through the eclipsey wood ---
      /*
      * Menu: HibAnalyzer
      * Script-Path: /Groovy Monkey Scripts/monkey/HibernateAnalyzer.gm
      * Kudos: alex
      * License: EPL 1.0
      * Exec-Mode: Foreground
      * DOM: http://groovy-monkey.sourceforge.net/update/plugins/net.sf.groovyMonkey.dom
      */

      import groovy.util.*
      import org.apache.commons.lang.exception.*

      monitor.beginTask( '', 1) 
      if( monitor.isCanceled() )
          return

      def s = """
      <hibernate-mapping>\n
      <class name="test">\n
      <property name="txt" type="java.lang.String">\n
      <column name="TXT" sql-type="VARCHAR2(50)" not-null="true"/>\n
      </property>\n
      </class>\n
      </hibernate-mapping>\n
      """

      out.clear()
      def slurper = new XmlSlurper( false, false )
      try
      {
          def mapping = slurper.parseText( s.toString() )
          out.println "mapping: ${mapping.dump()}"
      }
      catch( Exception ex )
      {
          out.println ex.getMessage()
          out.println ExceptionUtils.getFullStackTrace( ex )
      }

      monitor.worked( 1 )

      monitor.done()

      --- And burbled as it ran! ---
      </pre>

       
    • I am ashamed.

      Thanks,
      James.

       
    • James E. Ervin
      James E. Ervin
      2006-09-18

      Don't worry about it eh?  It happens to the best of us, I am just happy it is working for you.

      Btw if you do not catch the exception you should be able to copy and paste to the clipboard from the dialog that pops up.