How to modify and XML on the fly? - node.js

I am reading a XML file and send it to a REST API. However before sending it I would like to modify some values.
This is how I send the data:
data = await readFile(path.resolve(__dirname, file), 'utf8');
const config = {
headers: {
'Content-Type': 'text/plain',
'Content-Length': data.length,
},
};
result = await axios.post(
'https://someRestapi.com/',
data, config,
);
And I want to change the name Simon to Zimon in the author name for example.
<?xml version="1.0" encoding="UTF-8"?>
<Document schemaVersion="12"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Author>
<Name>
<First>Simon</First>
<Second>SomeName</Second>
</Name>
</Author>
</Document>
Is there an easy solution to do this?

Had to do something similar recently and ended up using fast-xml-parser. Applied to your case, you could do:
const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
<Document schemaVersion="12"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Author>
<Name>
<First>Simon</First>
<Second>SomeName</Second>
</Name>
</Author>
</Document>`;
const xmlToJsonParser = require('fast-xml-parser');
const J2xParser = require("fast-xml-parser").j2xParser;
const tObj = xmlToJsonParser.getTraversalObj(xmlString,{ignoreAttributes :false});
const jsonObj = xmlToJsonParser.convertToJson(tObj,{ignoreAttributes :false});
jsonObj.Document.Author.Name.First = "Zimon";
let result = new J2xParser({format:true, ignoreAttributes :false}).parse(jsonObj);
result = `<?xml version="1.0" encoding="UTF-8"?>\n${result}`;
console.log(result);
This will print:
<?xml version="1.0" encoding="UTF-8"?>
<Document schemaVersion="12" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Author>
<Name>
<First>Zimon</First>
<Second>SomeName</Second>
</Name>
</Author>
</Document>

Related

Issue while converting xml to json, and then back to xml

I'm facing an issue when converting XML to JSON and then JSON to XML.
I'm using this xml-js npm library.
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<property name="DESCRIPTION" value="Group 1 & , " , <"/>
test.js
const convert = require('xml-js');
const fs = require('fs');
const xml = fs.readFileSync('data.xml',).toString();
const result = convert.xml2json(xml, {compact: false, spaces: 2});
const output = convert.json2xml(result,{compact: false, spaces: 2});
fs.writeFileSync('output.xml', output);
output.xml
<?xml version="1.0" encoding="UTF-8"?>
<property name="DESCRIPTION" value="Group 1 & , " , <"/>
I was expecting to get the below result in output.xml
expected output.xml
<?xml version="1.0" encoding="UTF-8"?>
<property name="DESCRIPTION" value="Group 1 & , " , <"/>
Any idea why I'm getting that data on converting from JSON to XML.
I was able to get the problem fixed by using attributeValueFn.
const convert = require('xml-js');
const fs = require('fs');
const xml = fs.readFileSync('data.xml',).toString();
const result = convert.xml2json(xml, {compact: false, spaces: 2,
attributeValueFn: function(value) {
return value.replace(/&/g,'AMPERSAND_CHARACTER');
}
});
console.log('JSON : ' + result);
const output = convert.json2xml(result,{compact: false, spaces: 2,
attributeValueFn: function(value) {
return value.replace(/AMPERSAND_CHARACTER/g, '&')
.replace(/</g, '<');
}
});
fs.writeFileSync('output.xml', output);

Unable to construct a SOAP request with NodeJS

The WSDL in question is under the following URL: https://bmdweb.bmd.at/bmdntcswsdev/bmdntcsws.dll/wsdl/IBMDSOAPNTCS
I was provided with a sample log-output of how the XML should be constructed.
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.bmd.at/" xmlns:types="http://www.bmd.at/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<q1:Login xmlns:q1="http://www.bmd.at/wws">
<ALoginRequest href="#id1" />
</q1:Login>
<q2:TBMDLoginRequest id="id1" xsi:type="q2:TBMDLoginRequest" xmlns:q2="http://www.bmd.at/soap">
<Auth href="#id2" />
</q2:TBMDLoginRequest>
<q3:TBMDPlainText id="id2" xsi:type="q3:TBMDPlainText" xmlns:q3="http://www.bmd.at/soap">
<Username xsi:type="xsd:string">xxxxUSER</Username>
<Password xsi:type="xsd:string">xxxxPASSWORD</Password>
<DBAlias xsi:type="xsd:string">xxxxDBALIAS</DBAlias>
</q3:TBMDPlainText>
</soap:Body>
</soap:Envelope>
Problem
I simply cannot figure out how to construct this XML using https://www.npmjs.com/package/soap. The part, where ALoginRequest is within the Login element, while everything else is outside of it, confuses me.
Efforts
When I try this: const login = await client.LoginAsync({ALoginRequest: {Auth: {TBMDPlainText: auth}}}), I get the following output.
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://www.bmd.at/" xmlns:ns1="http://www.bmd.at/soap" xmlns:ns2="http://www.bmd.at/wws"><soap:Body><tns:Login><ALoginRequest><Auth><TBMDPlainText><Username>xxxx</Username><Password>xxxx</Password><DBAlias>xxxx</DBAlias></TBMDPlainText></Auth></ALoginRequest></tns:Login></soap:Body></soap:Envelope>
Can anyone provide me any hints?

sample Soap Message that calls a SavedSearch by the ID

I was wondering if someone has an example using SuiteTalk, i was searching but could not find any examples.
Here's a SOAP request that executes a customer search with the internal ID of the search.
<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<tokenPassport xmlns="urn:messages_2019_2.platform.webservices.netsuite.com">
<account xmlns="urn:core_2019_2.platform.webservices.netsuite.com">**********</account>
<consumerKey xmlns="urn:core_2019_2.platform.webservices.netsuite.com">****************************************************************</consumerKey>
<token xmlns="urn:core_2019_2.platform.webservices.netsuite.com">****************************************************************</token>
<nonce xmlns="urn:core_2019_2.platform.webservices.netsuite.com">b591c9bfb56c406c9cf79252783dac7f</nonce>
<timestamp xmlns="urn:core_2019_2.platform.webservices.netsuite.com">1605312585</timestamp>
<signature algorithm="HMAC_SHA256" xmlns="urn:core_2019_2.platform.webservices.netsuite.com">********************************************</signature>
</tokenPassport>
</soap:Header>
<soap:Body>
<search xmlns="urn:messages_2019_2.platform.webservices.netsuite.com">
<searchRecord savedSearchId="366" xsi:type="q1:CustomerSearchAdvanced" xmlns:q1="urn:relationships_2019_2.lists.webservices.netsuite.com"/>
</search>
</soap:Body>
</soap:Envelope>
It looks like this with C#
var search = new CustomerSearchAdvanced()
{
savedSearchId = "366"
};
var results = ns.search(search);

How do you add attribute to array element with xml2js?

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

Cannot parse kml file to be treated as xml

I want to parse a kml file to be treated like a xml file :
var parser = require('xml2json');
router.get('/', function (req, res) {
var xml = "D:/Axes2019.kml";
var json = parser.toJson(xml);
console.log("to json -> %s", json);
res.render("index");
});
At runtime I get There are errors in your xml file: not well-formed (invalid token)
Here is the kml :
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="Axes_2K19" id="Axes_2K19">
<SimpleField name="Name" type="string"></SimpleField>
<SimpleField name="Denomin" type="string"></SimpleField>
</Schema>
<Folder><name>Axes_2K19</name>
<Placemark>
<name>9O_6</name>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData>
<SimpleData name="Denomin">Antanimena</SimpleData>
</SchemaData></ExtendedData>
<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>47.5282959843938,-18.8881931524494 47.5273060204005,-18.8883147269749 47.525771678897,-18.8898762249651 47.525391488082,-18.8909217497064 47.5247125759123,-18.8915497434633 47.5244138545577,-18.8919299342784 47.5243188068539,-18.8924866422575 47.5259255656555,-18.8935536658841 47.5268466231657,-18.8952758397545 47.5265207453242,-18.8961312690883 47.5276816851343,-18.8957442891516 47.5279871956107,-18.8964978816599 47.529947,-18.89907 47.52965,-18.901435 47.5292802275261,-18.9017841358174 47.5286065539348,-18.9019002864366 47.5276076586099,-18.9018770563128 47.5269339850186,-18.9021906629845 47.5268062193375,-18.9027481859566 47.526667,-18.903358 47.526247,-18.903399 47.525913,-18.903094 47.525489,-18.903003 47.524572,-18.902694 47.523774,-18.902588 47.523368,-18.9026 47.522878,-18.902871 47.522666,-18.903034 47.5223273311563,-18.9027936605133 47.521073,-18.901036 47.520669,-18.900556 47.518254,-18.89732 47.5166722429164,-18.8951662207165 47.516752829692,-18.8950896025168 47.5168139317873,-18.8943258263259 47.5166917275968,-18.8930121312776 47.5173638506447,-18.8917595383246 47.5187879970001,-18.8904753419999 47.516371167,-18.887820824 47.5151030731197,-18.8858937371787 47.5153169304532,-18.8838162659395 47.5165084213109,-18.8805396660807 47.5159890535012,-18.8784851081272 47.5159886221055,-18.8784787989658 47.5193198602536,-18.8783563269751 47.521029,-18.877701 47.5222222097789,-18.8787229395467 47.5236810223034,-18.8789367968802 47.526064004019,-18.8796089199281 47.529757529473,-18.8805918742828 47.5292090646449,-18.8815425466514 47.5290563094068,-18.8836505689382 47.5292090646449,-18.8857662289869 47.5285674926446,-18.8872937813686 47.5282959843938,-18.8881931524494</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
</Placemark>
...
</Folder>
</Document></kml>
So how to fix it ?
I checked your sample KML for XML and KML Schema validity, and it looked fine to me. Maybe your parser wants more of the namespaces defined? See below for what I usually include at the top of my KMLs:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
...
</kml>
Or maybe some of the rest of your file (which you replaced with "..." contains XML errors? Make sure all your IDs are valid too (start with a letter, no forbidden characters, etc.).

Resources