XMLHTTPRequest - can't do anything with response - cross-domain

I'm new to Javascript, but we all was at sometime. I'm Stuck!
I can get my XMLHTTPRequest response ok from a gov site in xml format (no JSON available). I can't get it to console log or display to my html page.
I can only see it in the firefox>developertools>network>response
What am I doing wrong?
I want to see it in my console log and get it to my html page.
MY RESPONSE - IN FIREFOX: >developerTools>network>response
<?xml version="1.0" encoding="UTF-8"?>
<response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:noNamespaceSchemaLocation="http://www.aviationweather.gov/static/adds/schema/metar1_2.xsd">
<request_index>174466304</request_index>
<data_source name="metars" />
<request type="retrieve" />
<errors />
<warnings />
<time_taken_ms>14</time_taken_ms>
<data num_results="6">
<METAR>
<raw_text>KCRX 161655Z AUTO 29010KT 9SM BKN012 OVC017 03/00 A3000 RMK AO2</raw_text>
<station_id>KCRX</station_id>
<observation_time>2020-12-16T16:55:00Z</observation_time>
<latitude>34.92</latitude>
<longitude>-88.6</longitude>
<temp_c>3.0</temp_c>
<dewpoint_c>0.0</dewpoint_c>
<wind_dir_degrees>290</wind_dir_degrees>
<wind_speed_kt>10</wind_speed_kt>
<visibility_statute_mi>9.0</visibility_statute_mi>
<altim_in_hg>30.0</altim_in_hg>
<quality_control_flags>
<auto>TRUE</auto>
<auto_station>TRUE</auto_station>
</quality_control_flags>
<sky_condition sky_cover="BKN" cloud_base_ft_agl="1200" />
<sky_condition sky_cover="OVC" cloud_base_ft_agl="1700" />
<flight_category>MVFR</flight_category>
<metar_type>METAR</metar_type>
<elevation_m>128.0</elevation_m>
</METAR>
</data>
</response>
MY XMLHTTPRequest:
const request = new XMLHttpRequest();
request.open('GET', 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=KCRX&hoursBeforeNow=2',);
request.onload = function() {
console.log(request);
};
request.send();
My Console shows:
enter image description here
MY Header:
enter image description here

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?

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

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

Using Smooks or Groovy with Java Camel to split/transform XML

Camel version 2.14 Smooks version 1.5.1
I got a message which i want to split and transform, but i need the id from the parent. So I thought about using Smooks, splitting the message, transforming and send each output to a queue. Which will be using freemarker template for the transform.
<!-- Message -->
<data>
<id>123</id> <!-- This is needed in both portal messages -->
<portals>
<portal id="1" />
<portal id="2" />
</portals
</data>
<!-- Msg 1 -->
<portal dataId="123">
<id>1</id>
<portal>
<!-- Msg 2 -->
<portal dataId="123">
<id>2</id>
<portal>
There are plenty of examples. But for example the camel examples does not work, due to "java.lang.ClassNotFoundException: org.apache.camel.component.ResourceBasedComponent" which is a known issue.
An alternative would be using groovy for transformation?
So, how could this easiest be solved?
I don't know about smooks, but you can combine the XSLT transformer with a XPATH splitter to do this.
First, transform the data into the blocks that should make up each message. Do it using XSLT, groovy or whatever you feel comfortable with. Here is a simple stylesheet, to be put into src/main/resources (or any classpath location).
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<portals>
<xsl:variable name="dataId" select="/data/id"/>
<xsl:for-each select="/data/portals/portal">
<portal dataId="$dataId">
<xsl:attribute name="dataId">
<xsl:value-of select="/data/id"/>
</xsl:attribute>
<id><xsl:value-of select="#id"/></id>
</portal>
</xsl:for-each>
</portals>
</xsl:template>
The Camel route: First the transform, then splitter. The "to" can be whatever, like a seda/direct for further processing or the target protocol.
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file:data"/>
<to uri="xslt:transform.xslt"/>
<split>
<xpath>portals/portal</xpath>
<to uri="log:foo.bar?level=INFO"/>
</split>
</route>
In groovy it can be done this way:
import groovy.util.XmlSlurper
import groovy.xml.MarkupBuilder
def xml = """
<data>
<id>123</id>
<portals>
<portal id="1" />
<portal id="2" />
</portals>
</data>
"""
def slurped = new XmlSlurper().parseText(xml)
def msgId = slurped.id
def portalIds = slurped.portals.portal.#id*.text()
def portalXmls = portalIds.collect { portalId ->
writer = new StringWriter()
portalXml = new MarkupBuilder(writer)
portalXml.doubleQuotes = true
portalXml.portal(dataId: msgId) {
id(portalId)
}
writer
}.each { println it.toString() }
null

Resources