Skip to content

Instantly share code, notes, and snippets.

@skhatri
Created April 22, 2012 06:11
Show Gist options
  • Save skhatri/2460575 to your computer and use it in GitHub Desktop.
Save skhatri/2460575 to your computer and use it in GitHub Desktop.
Using Groovy Namespace with slurper and builders
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