Created
April 22, 2012 06:11
-
-
Save skhatri/2460575 to your computer and use it in GitHub Desktop.
Using Groovy Namespace with slurper and builders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test | |
public class NamespaceTest { | |
@Test | |
public void testMarkupBuilderUsingNamespace() throws Exception { | |
final investmentNS = 'urn:investment:facility' | |
def path = new XmlSlurper() | |
.parseText(""" | |
<FacilityInvestment xmlns="${investmentNS}"> | |
<Fund> | |
<Name>GOOGL</Name> | |
<UnitsHeld>100</UnitsHeld> | |
</Fund> | |
<Fund> | |
<Name>CBA</Name> | |
<UnitsHeld>200</UnitsHeld> | |
</Fund> | |
</FacilityInvestment> | |
""") | |
.declareNamespace(fi:investmentNS) | |
StreamingMarkupBuilder builder = new StreamingMarkupBuilder(); | |
def ns = builder.bind { | |
mkp.declareNamespace(fi:investmentNS) | |
mkp.yield(path) | |
} | |
builder = new StreamingMarkupBuilder() | |
def node = builder.bind { | |
mkp.declareNamespace(fi:investmentNS) | |
fi.FacilityInvestment { | |
fi.Fund { | |
fi.Name('GOOGL') | |
fi.UnitsHeld(100) | |
} | |
fi.Fund { | |
fi.Name('CBA') | |
fi.UnitsHeld(200) | |
} | |
} | |
} | |
assert ns.toString() == node.toString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment