I am trying to fetch the namespace for an xml(xsd) file through Groovy.
The Node returned by the XmlParsers, and the GPathResult by XmlSlurper seems to ignore the namespace definitions.
As an example:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
trying to fetch the attributes of the rootNode through
rootNode.attributes()
will retrieve only:
targetNamespace:http://www.w3schools.com
elementFormDefault:qualified
and leave out the xml:ns and xmlns definitions. Same result is contained in the # attribute of GPathResult.
Seems this has nothing to do with the Node class implementation and relies on the XmlParsers used.
So how should the XmlParser be implemented to include these attributes in the node, and any reason this has been left outside of Groovy?
Use a different variant of XmlSlurper constructor:
def xml = """<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
</xs:schema>
"""
assert new XmlSlurper(false, false, false).parseText(xml).attributes() == [
'xmlns:xs':'http://www.w3.org/2001/XMLSchema',
'targetNamespace':'http://www.w3schools.com',
'xmlns':'http://www.w3schools.com',
'elementFormDefault':'qualified'
]
Related
I'm trying to figure out how to add attributes to an array element
I get an error Error: Invalid character in name when trying to build the following XML from an object.
<?xml version="1.0" encoding="UTF-8"?>
<Requests xmlns="http://example.com">
<Request>
</Request>
<Request>
</Request>
</Requests>
Here is my object
let myObject = {
Requests:[
{$:{xmlns:"http://example.com"}},
{Request:{}},
{Request:{}}
]
}
let builder = new xml2js.Builder();
let xml = builder.buildObject(myObject);
console.log(xml)
I've also tried wrapping the name in quotes
{"$":{"xmlns":"http://example.com"}},
Stripping out the attribute declaration altogether produces the desired XML but obviously leaves out the needed attribute
<?xml version="1.0" encoding="UTF-8"?>
<Requests>
<Request>
</Request>
<Request>
</Request>
</Requests>
I suppose you should try this:
let myObject = {
Requests:{
$:{xmlns:"http://example.com"},
Request:[{}, {}]
}
I am trying to get the value of the attribute IntegratorCode AND ProductType using groovy.
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAPNYK1="http://www.w3.org/2001/XMLSchema"
xmlns:SOAPNYK2="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAPNYK3="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Header>
<Authentication
xmlns="urn:iQQ:API:22:iQQMessages.xsd" EmployeeCode="*****" IntegratorCode="GENERIC" Pwd="******" Usr="SYSUSER"/>
</soap:Header>
<soap:Body>
<CanOfferProduct
xmlns="urn:iQQ:API:22:iQQMessages.xsd">
<OnQuotePackage
xmlns="" ProductType="GAP" QuoteNumber="74859">test
</OnQuotePackage>/>
</CanOfferProduct>
</soap:Body>
</soap:Envelope>
I tried the following method using XmlSlurpur
def result = new XmlSlurper().parseText(xml)
println(result.Envelope.Header[1].Authentication.#IntegratorCode)
//output = IntegratorCode
I wanted to print the value of IntegratorCode which is "GENERIC".
I am not sure what I am doing wrong in this case. please help.
what you called result is soap:Envelope, so you essentially had Envelope.Envelope... This works:
println result.Header.Authentication.#IntegratorCode
As a precaution I normally use it like this:
def Envelope = new XmlSlurper().parseText(xml)
I have a string describing a XML object, containing line-breaks and spaces, like so:
<?xml version="1.0" encoding="UTF-8"?>
<play>
<scenario>
<author>Arthur Drake</author>
<title> ...til I get there</title>
</scenario>
</play>
And I'd like to convert it like so all unnecessary characters are removed:
<?xml version="1.0" encoding="UTF-8"?><play><scenario><author>Arthur Drake</author><title> ...til I get there</title></scenario></play>
Note that the whitespaces inside the nodes should be left untouched.
What is the best solution to do so in Node.js ?
This code should give you the result you're looking for:
let xml =
`<?xml version="1.0" encoding="UTF-8"?>
<play>
<scenario>
<author>Arthur Drake</author>
<title> ...til I get there</title>
</scenario>
</play>`;
console.log('Initial xml:', xml);
function trimXml(xml) {
return xml.replace(/>\s+</g, "><");
}
console.log('\nTrimmed xml:', trimXml(xml));
I have an xml that looks like this
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Samples>
<Sample>
<Name>
Sample1
</Name>
<Date>
01/20/2016
</Date>
</Sample>
</Samples>
I want to simply change the tag name from "Samples" to "SampleList". How will I do it?
replaceNode can be used to rename the node as below:
def xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Samples>
<Sample>
<Name>
Sample1
</Name>
<Date>
01/20/2016
</Date>
</Sample>
</Samples>
'''
def result = new XmlSlurper().parseText(xml)
result.replaceNode {
'SampleList' it.children()
}
groovy.xml.XmlUtil.serialize(result)
replaceNode takes a closure as method parameter which delegates to a builder. Specifically in this case the node is replaced instead of appending it to main document. 'SampleList' it.children() is similar to 'SampleList(it.children())'.
Parsed xml's root element being Samples (which needs replacement), replaceNode was directly done on the result.
I'd like to parse an XML document (SOAP request message) for a particular element. The document is stored in requestContent and looks as follows:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:udb="http://somenamespace>
<soap:Header/>
<soap:Body>
<udb:ProvideUDBIdentityInformationRequest>
<udb:RequestID>1</udb:RequestID>
<udb:IDnumber>1</udb:IDnumber>
<udb:UnifiedNumber>3</udb:UnifiedNumber>
</udb:ProvideUDBIdentityInformationRequest>
</soap:Body>
</soap:Envelope>
My Groovy code looks like this:
def request = new XmlSlurper().parseText(requestContent)
println request.ProvideUDBIdentityInformationRequest.RequestID
However the output is empty whereas I would have expected a "1".
Thanks,
Robert
Can you try:
println request.Body.ProvideUDBIdentityInformationRequest.RequestID
(you also have a " missing from the end of the xml declaration, but I guess that's a cut/paste error?)