java - How to ignore xsi:nil attributes when parsing xml with XmlSlurper -


we're upgrading 3rd-party product consume xml content. new version generates xml xsi:nil="true" attributes, indicating null elements:

<?xml version="1.0" encoding="utf-8"?> <data xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">   <cusip  xsi:nil="true"/>   <ticker xsi:nil="true"/>   <year>2014</year> </data> 

when parsing, use:

def parsed = new xmlslurper().parsetext(xml) ... element.attributes().each{ k,v -> { } 

..but attribute key xsi:nil="true", comes as:

"{http://www.w3.org/2001/xmlschema-instance}nil"

...and raising hell our downstream processing because it's not expecting attribute key enclosed in braces.

does xmlslurper support way ignore xsi schema type attributes without having filter them out manually?

to clear

given xml...

<?xml version="1.0" encoding="utf-8"?> <data xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">   <cusip  xsi:nil="true"/>   <ticker xsi:nil="true"/>   <year format='yyyy'>2014</year> </data> 

...only attribute format visible; xsi:nil attributes ignored:

def parser1 = new xmlparser(false, false).parsetext(xml) assert parser1.children()*.attributes().size() == 1   // 'format' 

update:

you can use xmlslurper namespaceaware set false as:

def parsed = new xmlslurper(false, false).parsetext(xml) 

you can use xmlparser parsing, similar xmlslurper, if feasible. have option of making parser namespace unaware using below:

def parsed = new xmlparser(false, false).parsetext(xml) 

toggle second argument (namespaceaware) of constructor true see difference.

example:

def xml = '''<?xml version="1.0" encoding="utf-8"?> <data xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">   <cusip  xsi:nil="true"/>   <ticker xsi:nil="true"/>   <year>2014</year> </data> '''  def parser1 = new xmlparser(false, false).parsetext(xml) def parser2 = new xmlparser(false, true).parsetext(xml)  println parser1.children()*.attributes() println parser2.children()*.attributes() 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -