groovy
  1. groovy
  2. GROOVY-5434

Node.toString() returns an empty string

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.8.6, 2.4.0-beta-3
    • Fix Version/s: None
    • Component/s: groovy-jdk, XML Processing
    • Labels:
      None
    • Number of attachments :
      0

      Description

      The method toString() on Node seems to return an empty string on elements from an XML document read by XmlSlurper. It would be useful for debugging to at least print a unique identifier for all nodes. Another option would be to return a serialized version of itself.

        Activity

        Hide
        Uri Moszkowicz added a comment - - edited

        Test.groovy:

        import groovy.util.XmlSlurper
        def xmlRoot = new XmlSlurper().parse(new File("test.xml"))
        xmlRoot.'**'.grep { it.name() == "testcase" }.each { node ->
          println node.name()
          println node.@name
          println node.toString()
        }
        

        test.xml:

        <?xml version="1.0" encoding="UTF-8"?>
        <testsuites>
          <testsuite name="tests">
            <testcase name="test"/>
          </testsuite>
        </testsuites>
        

        output:

        testcase
        test
        
        
        

        JDK version: 1.6.0_14
        Groovy version: 2.0.0-beta-2

        Show
        Uri Moszkowicz added a comment - - edited Test.groovy: import groovy.util.XmlSlurper def xmlRoot = new XmlSlurper().parse(new File("test.xml")) xmlRoot.'**'.grep { it.name() == "testcase" }.each { node -> println node.name() println node.@name println node.toString() } test.xml: <?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name="tests"> <testcase name="test"/> </testsuite> </testsuites> output: testcase test JDK version: 1.6.0_14 Groovy version: 2.0.0-beta-2
        Hide
        Uri Moszkowicz added a comment -

        I think the problem here is that node is actually a GPathResult, which just returns text(). Both NodeChild and NodeChildren seem to use Node.text() for this value, which is often times an empty string. Why not use Node.toString() instead?

        Show
        Uri Moszkowicz added a comment - I think the problem here is that node is actually a GPathResult, which just returns text(). Both NodeChild and NodeChildren seem to use Node.text() for this value, which is often times an empty string. Why not use Node.toString() instead?
        Hide
        Paul King added a comment -

        There is an existing (quite old) issue to make XmlParser more like the current behavior of XmlSlurper (GROOVY-2812), i.e. call text() from toString(). This is for a single node rather than a node list but it seems some people quite like this behavior.

        Personally I would like not to invest too much time in the existing XML apis - apart from fixing very obvious bugs. After we get modules into a final release, I would like to work on a replacement XML api - which may borrow from the existing ones but would be a separate module that could have breaking behavior changes. But feel free to make suggestions/contributions as a new api is still some way off.

        Show
        Paul King added a comment - There is an existing (quite old) issue to make XmlParser more like the current behavior of XmlSlurper ( GROOVY-2812 ), i.e. call text() from toString(). This is for a single node rather than a node list but it seems some people quite like this behavior. Personally I would like not to invest too much time in the existing XML apis - apart from fixing very obvious bugs. After we get modules into a final release, I would like to work on a replacement XML api - which may borrow from the existing ones but would be a separate module that could have breaking behavior changes. But feel free to make suggestions/contributions as a new api is still some way off.
        Hide
        Pascal Schumacher added a comment - - edited

        sorry, didn't read the whole discussion

        Show
        Pascal Schumacher added a comment - - edited sorry, didn't read the whole discussion

          People

          • Assignee:
            Unassigned
            Reporter:
            Uri Moszkowicz
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: