Cannot parse kml file to be treated as xml - node.js

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

Related

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?

How to modify and XML on the fly?

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>

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.

groovy XmlSlurper parse XML

I get a XML file like this:
<?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>
I want to save the below string to another file.
<soap:Body>
<udb:ProvideUDBIdentityInformationRequest>
<udb:RequestID>1</udb:RequestID>
<udb:IDnumber>1</udb:IDnumber>
<udb:UnifiedNumber>3</udb:UnifiedNumber>
</udb:ProvideUDBIdentityInformationRequest>
</soap:Body>
How can I achieve that ?
In fact, I've refer to some tutorial like this one ,http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper, but cannot find such a method in XmlSlurper.
Thanks in advance!
Assuming you have the original xml in a variable called xml, you can do:
String output = new groovy.xml.StreamingMarkupBuilder().bind {
mkp.yield new XmlSlurper().parseText( xml ).Body
}
Then, output equals:
<soap:Body xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>
<udb:ProvideUDBIdentityInformationRequest xmlns:udb='http://somenamespace'>
<udb:RequestID>1</udb:RequestID>
<udb:IDnumber>1</udb:IDnumber>
<udb:UnifiedNumber>3</udb:UnifiedNumber>
</udb:ProvideUDBIdentityInformationRequest>
</soap:Body>

Resources