How do you add attribute to array element with xml2js? - node.js

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:[{}, {}]
}

Related

Groovy get xml tags attribute value

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)

How to inline a XML string (suppress all unnecessary characters) in Node.js

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));

Change xml element/tag name using XmlSlurper or XmlParser

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.

Find GPath exists in given XML document using Groovy

In Groovy given a GPath I want to find that the path exists in XML document. So that I can set a value for the node. I can use xmlslurper for parsing the document. GPath is a string path represented in slashes format.
Look at example:
def xml = '''
<?xml version="1.0" encoding="UTF-8"?>
<data>
<level0 id="1" t="0">
<level1 id="lev1id01" att1="2015-05-12" val="12" status="0" year="2015" month="05" />
</level0>
</data>
'''
// find all nodes named 'level1'
def level1Nodes = new XmlSlurper().parseText(xml).level0.level1
// display found nodes names
level1Nodes.each { node ->
println "node: ${node.name()}"
}
// Get 'year' attribute value for 'level1' node
def level1YearValue = level1Nodes.each { node ->
println "${node.#year}"
}
Could you be more specific in your question?

Reading XML element with XmlSlurper in Groovy

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?)

Resources