Fetch abstract type data from plist - plist

I have archived JSON data in plist as data abstract type.
My plist format is :
`<dict>
<key>
<array>
<data>
4352ght4 qwe435g5 yu67k10p
</data>
</array>
</key>
<key>
<array>
<data>
4352ght4 qwe435g5 yu67k10p
</data>
</array>
</key>
<key>
<array>
<data>
4352ght4 qwe435g5 yu67k10p
</data>
</array>
</key>
</dict>`
This data type contains long JSON entries. How do I fetch data from this property list and display them in the table.
I have also prepared .h and .m classes to fetch data.

NSString *plistFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"plist.plist"];
NSDictionary *list = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
NSLog(#"%#",list);
for (id key in list) {
NSArray *array = [list objectForKey:key];
timesheetObject *timeobject = [timesheetObject alloc];
for(int i=0;i<array.count ;i++)
{
timeobject = (timesheetObject *)[NSKeyedUnarchiver unarchiveObjectWithData:[array objectAtIndex:i]];
NSLog(#"%d",timeobject.taskId);
NSLog(#"Start time %#",timeobject.startTime_actual);
NSLog(#"End Time %#",timeobject.endTime_actual);
[self.objectHolderArray addObject:timeobject];

Related

WSO2 : Transforming response xml

I would like to turn this xml response into something more easily readable.
<?xml version="1.0" encoding="ISO-8859-1"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
<soap:Body>
<executeResponse xmlns="urn:GCE">
<BusinessViewServiceexecuteOut xmlns="http://www.generix.fr/technicalframework/businesscomponent/applicationmodule/common" xmlns:ns2="http://www.generixgroup.com/processus/configuration/scheduler" xmlns:ns3="http://www.generix.fr/technicalframework/business/service/common">
<xmlpres><?xml version = '1.0' encoding = 'UTF-8'?> <VueTable type="View" name="Table" habctr="true" total_business_row="2" nbline="400" confNbline="400" numpage="1" nbpage="1">
<JTblView name="JTblView" type="ViewObject" maxfetchsize="999" maxfetchsizeexceeded="false">
<JTblViewRow current="true" type="ViewRow" index="1" business_row_index="1">
<Cletbl precision="6" type="VARCHAR" pk="true">
<business_data>N</business_data>
</Cletbl>
<Codtbl precision="6" type="VARCHAR" pk="true">
<business_data>001</business_data>
</Codtbl>
<Lib1 precision="30" type="VARCHAR">
<business_data>Non</business_data>
</Lib1>
<Lib2 precision="30" type="VARCHAR">
<business_data/>
</Lib2>
<Lir precision="10" type="VARCHAR">
<business_data>Non</business_data>
</Lir>
</JTblViewRow>
<JTblViewRow type="ViewRow" index="2" business_row_index="2">
<Cletbl precision="6" type="VARCHAR" pk="true">
<business_data>O</business_data>
</Cletbl>
<Codtbl precision="6" type="VARCHAR" pk="true">
<business_data>001</business_data>
</Codtbl>
<Lib1 precision="30" type="VARCHAR">
<business_data>Oui</business_data>
</Lib1>
<Lib2 precision="30" type="VARCHAR">
<business_data/>
</Lib2>
<Lir precision="10" type="VARCHAR">
<business_data>Oui</business_data>
</Lir>
</JTblViewRow>
</JTblView>
</VueTable></xmlpres>
</BusinessViewServiceexecuteOut>
</executeResponse>
</soap:Body></soap:Envelope>
At least if I could extract what's in the value of "xmlpres", the better I could do:
<table><row><code></code><libelle></libelle/></row></table>
To then turn it into a json response but I can't see ... I just get all the output or in json stream but with everything , which is not usable.
Create an out-mediation sequence with the following content and attach it to the respective API and try out the scenario. This is to extract the xmlpres content and send that as the response to the client
<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="out-sequence">
<!-- extract the xmlpres content and store as OM element -->
<property name="XMLBody"
expression="$body//soap:Body//generic:xmlpres"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:gce="urn:GCE"
xmlns:generic="http://www.generix.fr/technicalframework/businesscomponent/applicationmodule/common" type="OM" />
<!-- pass the extracted property as response body -->
<enrich>
<source type="property" property="XMLBody" />
<target type="body" />
</enrich>
</sequence>
Hope this helps you to extract and send the response accordingly.

Writing to XML failing abruptly

We have C on windows program in which we are writing some data we call as test results to an xml file.
We use libxml2-2.9.4 API to parse and write to XML.
Each test is described by its test tag "test_info" and the test result with tag "result".
We write each test along with its result as follows:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="style/results.xsl" type="text/xsl"?>
<all_test_result>
<test_info TestNumber="1" TestName="test1" Duration="90s" TestServer="s1">
<result FaultCount="0" TestRunTime="0.00" testResult="success"/>
....
<test_info TestNumber="13" TestName="test14" Duration="90s" TestServer="s1">
<result FaultCount="0" TestRunTime="0.00" testResult="success"/>
<test_info TestNumber="14" TestName="test15" Duration="90s" TestServer="s1">
<result FaultCount="0" TestRunTime="0.00" testResult="success"/>
<test_info TestNumber="15" TestName="test16" Duration="90s" TestServer="s1">
<result FaultCount="0" TestRunTime="0.00" testResult="success"/>
</all_test_result>
We use function "xmlSaveFormatFileEnc" to write to XML.
Code snippet:
xmlDocPtr outDoc = NULL;
xmlNodePtr np = NULL;
xmlNodePtr rootnode = NULL;
xmlNodePtr testinfonode = NULL;
LIBXML_TEST_VERSION
resXml = xmlNewDoc(BAD_CAST "1.0");
if (resXml != NULL)
{
rootnode = xmlNewDocNode(resXml, NULL, BAD_CAST "all_test_result", NULL);
xmlDocSetRootElement(resXml, rootnode);
testinfonode = xmlNewChild(rootnode, NULL, BAD_CAST "test_info", NULL);
....
xmlNewProp(testinfonode, BAD_CAST "TestName", testName);
...
np = xmlNewDocPI(resXml, BAD_CAST "xml-stylesheet", BAD_CAST "href=\"style/results.xsl\" type=\"text/xsl\"");
xmlAddPrevSibling(xmlDocGetRootElement(resXml), np);
xmlSaveFormatFileEnc(resultXmlFile, resXml, MY_ENCODING, 1);
...
The test xml file is written every 1 second and some 0 to 200 tests tags along with their result tags are written each time in the file.
Many times the result xml file is written properly and completely.
Some times the file is not written completely and stops abrubptly like it will write incomplete file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="style/results.xsl" type="text/xsl"?>
<all_test_result>
<test_info TestNumber="1" TestName="test1" Duration="90s" TestServer="s1">
<result FaultCount="0" TestRunTime="0.00" testResult="success"/>
<test_info TestNumber="13" TestName="test14" Duration="90s" TestServer="s1">
<result FaultCount="0" TestRunTime="0.00" testResult="success"/>
<test_info TestNumber="14" TestName="test15" Duration="90s" TestServer="s1">
<result FaultCount="0" TestRunTime="0.00"
And then we get the error:
error : string is not in UTF-8
error : string is not in UTF-8
error : string is not in UTF-8
error : string is not in UTF-8
error : string is not in UTF-8
error : string is not in UTF-8
I/O error : encoder error
Any suggestion why is this happening and how to resolve this error?

Parse HTML/XML characters in R

I am reading in the following XML as a text file in R:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE score-partwise PUBLIC
"-//Recordare//DTD MusicXML 3.0 Partwise//EN"
"http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="3.0">
<part-list>
<score-part id="P1">
<part-name>Music</part-name>
</score-part>
</part-list>
<part id="P1">
<measure number="1">
<attributes>
<divisions>1</divisions>
<key>
<fifths>0</fifths>
</key>
<time>
<beats>4</beats>
<beat-type>4</beat-type>
</time>
<clef>
<sign>G</sign>
<line>2</line>
</clef>
</attributes>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<type>whole</type>
</note>
</measure>
</part>
</score-partwise>
R:
library(readtext)
xml <- readtext("musicxml.txt")$text
I am then trying to render this in Javascript via Shiny by feeding my XML text to a Javascript function. NB: Working outside of R.
shiny::tags$script(paste0('var osmd = new opensheetmusicdisplay.OpenSheetMusicDisplay(\"sheet-music\", {drawingParameters: "compact",
drawPartNames: false, drawMeasureNumbers: false, drawMetronomeMarks: false, drawTitle: false});
var loadPromise = osmd.load(\'',xml,'\');
loadPromise.then(function(){
osmd.render();
});
'))
However, when I concatenate the XML string above, it does not work because characters are escaped, e.g one line:
<note>
I tried using the unescape_xml function here (with and without the tags removed), but this does not solve the problem. It leaves me with:
"Music1044G2C44whole"
So how can I end up with a concatenated string with none of the escaped characters? It must just be a string and not another R object.
You need to wrap the contents of the tag call with shiny::HTML to ensure it is passed unescaped:
shiny::tags$script(shiny::HTML(paste0(
'var osmd = new opensheetmusicdisplay.OpenSheetMusicDisplay(\"sheet-music\",
{ drawingParameters: "compact",
drawPartNames: false,
drawMeasureNumbers: false,
drawMetronomeMarks: false,
drawTitle: false});
var loadPromise = osmd.load(\'',xml,'\');
loadPromise.then(function(){ osmd.render() });')))
Which gives you:
<script>var osmd = new opensheetmusicdisplay.OpenSheetMusicDisplay("sheet-music",
{ drawingParameters: "compact",
drawPartNames: false,
drawMeasureNumbers: false,
drawMetronomeMarks: false,
drawTitle: false});
var loadPromise = osmd.load('<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE score-partwise PUBLIC
"-//Recordare//DTD MusicXML 3.0 Partwise//EN"
"http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="3.0">
<part-list>
<score-part id="P1">
<part-name>Music</part-name>
</score-part>
</part-list>
<part id="P1">
<measure number="1">
<attributes>
<divisions>1</divisions>
<key>
<fifths>0</fifths>
</key>
<time>
<beats>4</beats>
<beat-type>4</beat-type>
</time>
<clef>
<sign>G</sign>
<line>2</line>
</clef>
</attributes>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<type>whole</type>
</note>
</measure>
</part>
</score-partwise>');
loadPromise.then(function(){ osmd.render() });</script>

XML to list and back to XML

Situation: "Software" to R and back to "Software". The only interface for "Software" is xml.
In R, I need to make a few changes in the file so i convert it to a list and make some changes.
library(XML)
myFile = xmlParse("myXML")
xml_data <- xmlToList(myFile)
xml_data$timetable$train$.attrs[6] = "HelloNewWorld"
Now i need to convert this list "xml_data" it back to xml.
I found some functions like this:
function(item, tag) {
# just a textnode, or empty node with attributes
if(typeof(item) != 'list') {
if (length(item) > 1) {
xml <- xmlNode(tag)
for (name in names(item)) {
xmlAttrs(xml)[[name]] <- item[[name]]
}
return(xml)
} else {
return(xmlNode(tag, item))
}
}
# create the node
if (identical(names(item), c("text", ".attrs"))) {
# special case a node with text and attributes
xml <- xmlNode(tag, item[['text']])
} else {
# node with child nodes
xml <- xmlNode(tag)
for(i in 1:length(item)) {
if (names(item)[i] != ".attrs") {
xml <- append.xmlNode(xml, listToXml(item[[i]], names(item)[i]))
}
}
}
# add attributes to node
attrs <- item[['.attrs']]
for (name in names(attrs)) {
xmlAttrs(xml)[[name]] <- attrs[[name]]
}
return(xml)
}
But this doesnt work...
Any help or hints appreciated!
Thanks!
In the linked picture you can see the current xml-file. Highlighted in yellow the values that I need to change.
Link:
https://i.stack.imgur.com/remzj.png
Consider XSLT, the special-purpose language designed to transform XML files. No need to rewrite the entire tree in R. Using its xslt package (available on CRAN-R), extension of xml2, you can transform an input source and write output to screen or file.
Using the Identity Transform to copy document as is, below XSLT then rewrites one of the attributes in <train> tag, #source, similar to your above code attempt but with sixth attribute.
XML (sample input from railIML Wiki page)
<?xml version="1.0" encoding="UTF-8"?>
<railml xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="timetable.xsd">
<timetable version="1.1">
<train trainID="RX 100.2" type="planned" source="opentrack">
<timetableentries>
<entry posID="ZU" departure="06:08:00" type="begin"/>
<entry posID="ZWI" departure="06:10:30" type="pass"/>
<entry posID="ZOER" arrival="06:16:00" departure="06:17:00" minStopTime="9" type="stop"/>
<entry posID="WS" departure="06:21:00" type="pass"/>
<entry posID="DUE" departure="06:23:00" type="pass"/>
<entry posID="SCW" departure="06:27:00" type="pass"/>
<entry posID="NAE" departure="06:29:00" type="pass"/>
<entry posID="UST" arrival="06:34:30" type="stop"/>
</timetableentries>
</train>
</timetable>
</railml>
XSLT (save as .xsl file, rewrites the #source attribute)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="#source">
<xsl:attribute name="source">HelloNewWorld</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
R
library(xslt)
doc <- read_xml("/path/to/Input.xml", package = "xslt")
style <- read_xml("/path/to/XLSTScript.xsl", package = "xslt")
new_xml <- xml_xslt(doc, style)
# OUTPUT TO SCREEN
cat(as.character(new_xml))
# OUTPUT TO FILE
write_xml(new_xml, "/path/to/Output.xml")
Output
<?xml version="1.0" encoding="UTF-8"?>
<railml xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="timetable.xsd">
<timetable version="1.1">
<train trainID="RX 100.2" type="planned" source="HelloNewWorld">
<timetableentries>
<entry posID="ZU" departure="06:08:00" type="begin"/>
<entry posID="ZWI" departure="06:10:30" type="pass"/>
<entry posID="ZOER" arrival="06:16:00" departure="06:17:00" minStopTime="9" type="stop"/>
<entry posID="WS" departure="06:21:00" type="pass"/>
<entry posID="DUE" departure="06:23:00" type="pass"/>
<entry posID="SCW" departure="06:27:00" type="pass"/>
<entry posID="NAE" departure="06:29:00" type="pass"/>
<entry posID="UST" arrival="06:34:30" type="stop"/>
</timetableentries>
</train>
</timetable>
</railml>
I found it hard to apply many of the answers listed here so I wonder if this set of simple Java XML XPathHelper Unities may help others. You can find the source code here. I didn't write it all myself but adapted code I found, so I can't take all the credit but it works and it is compact and hope it helps others.
String xmlPayLoad = readFileAsString(payLoadPath + "/payLoad.xml");
TreeMap<String, String> header = new TreeMap<String, String>();
XPathHelperCommon xph = new XPathHelperCommon();
header = xph.findMultipleXMLItems(xmlPayLoad, "//header/*");
header.put("type", "newProcess");
xmlPayLoad = xph.modifyMultipleXMLItems(xmlPayLoad, "//header/*", header);
The primative XML header could be something like this:
<header>
<type>process</type>
<ruleBaseVersion>0</ruleBaseVersion>
<ruleBaseCommitment>0</ruleBaseCommitment>
<sequenceId>0</sequenceId>
<priortiseSID>0</priortiseSID>
<monitorIncomingEvents>0</monitorIncomingEvents>
<activityCount>0</activityCount>
<taskElapsedTime>0</taskElapsedTime>
<processStartTime>0</processStartTime>
<processElapsedTime>0</processElapsedTime>
<eventElapsedTime>0</eventElapsedTime>
<status>0</status>
</header>

Set the value of an object in a complex plist file

I want to change the value of an object in a Dictionary, however, the Dictionary structure is complex. That is;
<dict>
<key>Content</key>
<array>
<dict>
<key>Description</key>
<string></string>
<key>Duration</key>
<string></string>
<key>Image</key>
<string></string>
</dict>
<dict>
<key>Description</key>
<string></string>
<key>Duration</key>
<string></string>
<key>Image</key>
<string></string>
</dict>
</array>
</dict>
I can access an element with (Where the plist file is called Data)
[Data objectForKey:[[[Data objectForKey:#"Content"]objectAtIndex:selectedIndex]objectForKey:#"Description"]]
How do I set the value at for this object?
You can use this:
[[[Data objectForKey:#"Content"] objectAtIndex:selectedIndex] setObject:#"foo" forKey:#"Description"];
It set the string foo for the key Description into array at index selectedIndex into the dictionary with key Contents.

Resources