Swift String encoding and NSXMLParser parsing issues - string

My App is calling the free Weather Forecast web service found at this URL:
http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=Boston&CountryName=United+States
I'm using the usual NSURLConnection and NSXMLParser delegate methods to parse the incoming data (I've done this a million times before) but quite strangely, the NSMutableData that is returned is not getting converted to a string correctly via NSUTF8StringEncoding. Its basically failing to convert the "<" and ">" characters of the opening and closing XML tags, giving me "& l t;" and "& g t;" instead.
The problem seems to be in the connectionDidFinishLoading function:
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
webServiceData!.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection) {
let XMLResponseString = NSString(data: webServiceData!, encoding: NSUTF8StringEncoding)!
println("XMLResponseString = \(XMLResponseString)")
}
The output I get from the println statement there is:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.webserviceX.NET"><?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>DALLAS EXECUTIVE AIRPORT, TX, United States (KRBD) 32-41N 096-52W 203M</Location>
<Time>Dec 30, 2014 - 08:53 AM EST / 2014.12.30 1353 UTC</Time>
<Wind> from the NE (050 degrees) at 12 MPH (10 KT):0</Wind>
<Visibility> 9 mile(s):0</Visibility>
<SkyConditions> overcast</SkyConditions>
<Temperature> 39.9 F (4.4 C)</Temperature>
<DewPoint> 34.0 F (1.1 C)</DewPoint>
<RelativeHumidity> 79%</RelativeHumidity>
<Pressure> 30.42 in. Hg (1030 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather></string>
So as you can see I'm getting the first 2 tags correctly - the "< ?XML >" and "< string xmlns >" tags, but the rest are all showing up as "& l t;" and "& g t;"
What's really strange is that its saying encoding="utf-8" for the first tag, but on the second line (towards the end) its saying encoding="utf-16".
So I tried using NSUTF16StringEncoding:
let XMLResponseString = NSString(data: webServiceData!, encoding: NSUTF16StringEncoding)!
and that basically gave me chinese looking characters.
I also tried running the parser directly on the url instead of the NSMutableData that's returned, like so:
myXMLParser = NSXMLParser(contentsOfURL:theURL!)!
(the original statement was this:
myXMLParser = NSXMLParser(data:webServiceData)
but neither of these worked.
So what's going on here? Any suggestions on how to get this to work properly?

This is actually the remote service being broken, rather than your code. Yes, the server really is sending XML in XML for no particularly good reason.
$ curl 'http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=Boston&CountryName=United+States'
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.webserviceX.NET"><?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>BOSTON LOGAN INTERNATIONAL, MA, United States (KBOS) 42-22N 071-01W 54M</Location>

Related

Element:Text and sub elements combined in PowerBI & XML

Having an XML file like this:
<?xml version="1.0" encoding="UTF-8"?><outer>
<inner>Some text.</inner>
<inner>More text.</inner>
</outer>
and the following PowerBI script
let
Table0 = Xml.Tables(File.Contents("simple1.xml")){0}[Table]
in
Table0
you get this
Element:Text
Some text.
More text.
Now I'd like to add sub elements and keep inner.Element:Text
<?xml version="1.0" encoding="UTF-8"?><outer>
<inner>Some text.<secret>Don't care.</secret></inner>
<inner>More text.<secret>You know.</secret></inner>
</outer>
Using the same PowerBI script as above you get
secret
Don't care.
You know.
I already tried this script
let
Table0 = Xml.Tables(File.Contents("simple2.xml")),
Table1 = Table.ExpandTableColumn(Table0, "Table", {"secret"})
in
Table1
but got this
Name
secret
inner
Don't care.
inner
You know.
But I'd like to get this:
Element:Text
secret.Element:Text
Some text.
Don't care.
More text.
You know.
My current workaround (which I'd like to avoid) is to use sed to wrap the element text of an inner entry in its own sub element:
<inner><text>Some text.</text><secret>Don't care.</secret></inner>

Reading CDATA with lxml, problem with end of line

Hello I am parsing a xml document with contains bunch of CDATA sections. I was working with no problems till now. I realised that when I am reading the an element and getting the text abribute I am getting end of line characters at the beggining and also at the end of the text read it.
A piece of the important code as follow:
for comments in self.xml.iter("Comments"):
for comment in comments.iter("Comment"):
description = comment.get('Description')
if language == "Arab":
tag = self.name + description
text = comment.text
The problem is at element Comment, he is made it as follow:
<Comment>
<![CDATA[Usually made it with not reason]]>
I try to get the text atribute and I am getting like that:
\nUsually made it with not reason\n
I Know that I could do a strip and so on. But I would like to fix the problem from the root cause, and maybe there is some option before to parse with elementree.
When I am parsing the xml file I am doing like that:
tree = ET.parse(xml)
Minimal reproducible example
import xml.etree.ElementTree as ET
filename = test.xml #Place here your path test xml file
tree = ET.parse(filename)
root = tree.getroot()
Description = root[0]
text = Description.text
print (text)
Minimal xml file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Description>
<![CDATA[Hello world]]>
</Description>
You're getting newline characters because there are newline characters:
<Comment>
<![CDATA[Usually made it with not reason]]>
</Comment>
Why else would <![CDATA and </Comment start on new lines?
If you don't want newline characters, remove them:
<Comment><![CDATA[Usually made it with not reason]]></Comment>
Everything inside an element counts towards its string value.
<![CDATA[...]]> is not an element, it's a parser flag. It changes how the XML parser is reading the enclosed characters. You can have multiple CDATA sections in the same element, switching between "regular mode" and "cdata mode" at will:
<Comment>normal text <![CDATA[
CDATA mode, this may contain <unescaped> Characters!
]]> now normal text again
<![CDATA[more special text]]> now normal text again
</Comment>
Any newlines before and after a CDATA section count towards the "normal text" section. When the parser reads this, it will create one long string consisting of the individual parts:
normal text
CDATA mode, this may contain <unescaped> Characters!
now normal text again
more special text now normal text again
I thought that when CDATA comes at xml they were coming with end of line at the beginning and at the end, like that.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Description>
<![CDATA[Hello world]]>
</Description>
But you can have it like that also.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Description><![CDATA[Hello world]]></Description>
It is the reason to get end of line characters when we are parsing the with the Elementtree library, is working perfect in both cases, you only have to strip or not strip depending how you want to process the data.
if you want to remove both '\n' just add the following code:
text = Description.text
text = text.strip('\n')

Mapping excel to XML - Problem importing XML-fields

I seem to have a problem with mapping XML parts to an existing exceltable.
I have a sample XML file provided from the Swedish tax authority as XML-schema:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Skatteverket xmlns="http://xmls.skatteverket.se/se/skatteverket/ai/instans/infoForBeskattning/4.0"
xmlns:gm="http://xmls.skatteverket.se/se/skatteverket/ai/gemensamt/infoForBeskattning/4.0"
xmlns:ku="http://xmls.skatteverket.se/se/skatteverket/ai/komponent/infoForBeskattning/4.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" omrade="Kontrolluppgifter"
xsi:schemaLocation="http://xmls.skatteverket.se/se/skatteverket/ai/instans/infoForBeskattning/4.0
http://xmls.skatteverket.se/se/skatteverket/ai/kontrolluppgift/instans/Kontrolluppgifter_4.0.xsd ">
<ku:Avsandare>
<ku:Programnamn>KUfilsprogrammet</ku:Programnamn>
<ku:Organisationsnummer>162234567895</ku:Organisationsnummer>
<ku:TekniskKontaktperson>
<ku:Namn>Bo Ek</ku:Namn>
<ku:Telefon>+46881234567</ku:Telefon>
<ku:Epostadress>bo.ek#elbolagetab.se</ku:Epostadress>
<ku:Utdelningsadress1>Strömgatan 11</ku:Utdelningsadress1>
<ku:Postnummer>62145</ku:Postnummer>
<ku:Postort>Strömby</ku:Postort>
</ku:TekniskKontaktperson>
<ku:Skapad>2015-06-07T21:32:52</ku:Skapad>
</ku:Avsandare>
<ku:Blankettgemensamt>
<ku:Uppgiftslamnare>
<ku:UppgiftslamnarePersOrgnr>165599990602</ku:UppgiftslamnarePersOrgnr>
<ku:Kontaktperson>
<ku:Namn>John Ström</ku:Namn>
<ku:Telefon>+46812345678</ku:Telefon>
<ku:Epostadress>siv.strom#elbolagetab.se</ku:Epostadress>
<ku:Sakomrade>Förnybar el</ku:Sakomrade>
</ku:Kontaktperson>
</ku:Uppgiftslamnare>
</ku:Blankettgemensamt>
<!-- Kontrolluppgift 1 -->
<ku:Blankett nummer="2350">
<ku:Arendeinformation>
<ku:Arendeagare>165599990602</ku:Arendeagare>
<ku:Period>2018</ku:Period>
</ku:Arendeinformation>
<ku:Blankettinnehall>
<ku:KU66>
<ku:UppgiftslamnareKU66>
<ku:UppgiftslamnarId faltkod="201">165599990602</ku:UppgiftslamnarId>
<ku:NamnUppgiftslamnare faltkod="202">Sonjas elhandel</ku:NamnUppgiftslamnare>
</ku:UppgiftslamnareKU66>
<ku:Inkomstar faltkod="203">2018</ku:Inkomstar>
<ku:KWhMatatsIn faltkod="270">3622</ku:KWhMatatsIn>
<ku:KWhTagitsUt faltkod="271">4822</ku:KWhTagitsUt>
<ku:AnlaggningsID faltkod="272">735999123456789012</ku:AnlaggningsID>
<ku:AndelIAnslPunkt faltkod="273">12.5</ku:AndelIAnslPunkt>
<ku:Specifikationsnummer faltkod="570">128</ku:Specifikationsnummer>
<ku:InkomsttagareKU66>
<ku:Inkomsttagare faltkod="215">193804139149</ku:Inkomsttagare>
</ku:InkomsttagareKU66>
</ku:KU66>
</ku:Blankettinnehall>
</ku:Blankett>
</Skatteverket>
When using Excel, Developer tab -> XML -> Source and adding the file I don't seem to get the XML parts inside the tag
<ku:Blankettinnahall>
Any reason why Excel would skip these XML parts?
Here is some sample exceltable data that I would like to map to those XML-fields:
AnlaggningsID Inkomsttagare Inkomstar KWhMatatsIn KWhTagitsUt AndelIAnslPunkt Specifikationsnummer
526009875445385000 190101019999 2018 50078,0 88462,0 1
258655985101244000 190201019999 2018 75,0 4615,0 2
112855269388863000 190301019999 2018 16687,0 19870,0 42 3
364615095294089000 190401019999 2018 16687,0 19870,0 58 4
534980084130649000 190501019999 2018 174,0 7009,0 5
It looks like your missing the actual data itself...the top half is the description of the sender and details. And later is data section (Blankettinnehall)
So on your excel I would expect rows with columns for each header/ sender details. This may be whats missing.
You can see this if you take a sample file from them and view it in Excel.
I struggled with KU52 last year ended up doing a C# application to generate the XML file.

How to extract CDATA without the GPath/node name

I'm trying to extract CDATA content from an XML without the using GPath (or) node name. In short, i want to find & retrieve the innerText containing CDATA section from an XML.
My XML look like:
def xml = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<Test1>This node contains some innerText. Ignore This.</Test1>
<Test2><![CDATA[this is the CDATA section i want to retrieve]]></Test2>
</root>'''
From the above XML, i want to get the CDATA content alone without using the reference of its node name 'Test2'. Because the node name is not always the same in my scenario.
Also note that the XML can contain innerText in few other nodes (Test1). I dont want to retrieve that. I just need the CDATA content out of the whole XML.
I want something like below (the code below is incorrect though)
def parsedXML = new xmlSlurper().parseText(xml)
def cdataContent = parsedXML.depthFirst().findAll { it.text().startsWith('<![CDATA')}
My output should be :
this is the CDATA section i want to retrieve
As #daggett says, you can't do this with the Groovy slurper or parser, but it's not too bad to drop down and use the java classes to get it.
Note you have to set the property for CDATA to become visible, as by default it's just treated as characters.
Here's the code:
import javax.xml.stream.*
def xml = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<Test1>This node contains some innerText. Ignore This.</Test1>
<Test2><![CDATA[this is the CDATA section i want to retrieve]]></Test2>
</root>'''
def factory = XMLInputFactory.newInstance()
factory.setProperty('http://java.sun.com/xml/stream/properties/report-cdata-event', true)
def reader = factory.createXMLStreamReader(new StringReader(xml))
while (reader.hasNext()) {
if (reader.eventType in [XMLStreamConstants.CDATA]) {
println reader.text
}
reader.next()
}
That will print this is the CDATA section i want to retrieve
Considering you just have one CDATA in your xml split can help here
def xml = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<Test1>This node contains some innerText. Ignore This.</Test1>
<Test2><![CDATA[this is the CDATA section i want to retrieve]]></Test2>
</root>'''
log.info xml.split("<!\\[CDATA\\[")[1].split("]]")[0]
So in the above logic we split the string on CDATA start and pick the portion which is left after
xml.split("<!\\[CDATA\\[")[1]
and once we got that portion we did the split again and then got the portion which is before that pattern by using
.split("]]")[0]
Here is the proof it works

Delete all chars before the xml Tag in a string - in groovy. soapui

How do I replace all the characters with nothing (thus deleting them) up to a certain character? I have a log string which is an XML request:
I have a string like this:
Mon Dec 19 09:50:50 EST 2016:INFO:
string = "test-testing ID:idm-zx-sawe.3CE65834D32AD741:370 <?xml version="1.0" encoding="UTF-8"?>"
string.replaceAll("([^,]*'<')", "").replaceAll("(?m)^\\s*ID.*","");
I need to remove all the charters before <?xml
and return the following string: "test-testing ID:idm-zx-sawe.3CE65834D32AD741:370
I'm trying with this regular expression:
/.*<\?/ - need this translated to groovy string.replaceAll(".*<\?","")
I would do it like this:
​def string = 'test-testing ID:idm-zx-sawe.3CE65834D32AD741:370 <?xml version="1.0" encoding="UTF-8"?>'
def start = ​​​​​​​​​​​​​​​​string.indexOf('<?xml')​​​​​;
if (start) {
string = string.substring(start);
}​
string is:
<?xml version="1.0" encoding="UTF-8"?>

Resources