AWG Blogs

  • Coding without IF statements - Found below linked article that provides tips on how to avoid using IF statements, with one of the benefits being readability. The tips largely are based o...
    1 day ago
  • Microservices - A species would be a combination of roles [DCI?], instead of being characterized as an animal, which would not necessarily be the best description. At a ...
    2 weeks ago
  • CoR compared to Pipe and Filter - Java World implies the pipes-and-filters architectural style described by Parnas Software systems often employ the equivalent of pipes (e.g., email filter...
    7 months ago
  • Getting ADB Working for SPH-M840 - Had a SPH-M840 Galaxy Ring Virgin Mobile 3G Android version 4.1.2, attempting to install apps from Android Studio failed to detect device. Installed SAMSUN...
    1 year ago
  • How to check if I have write permissions to an Oracle table - SELECT CASE WHEN COUNT(*) > 0 THEN 'YES' ELSE 'NO' END AS PERMISSIONS FROM ( SELECT privilege FROM ( select * from dba_tab_privs where (grantee = 'MY_USE...
    1 year ago
  • Flyweight vs Singleton - Implementations seems to be virtually identical, differing only in style, where the flyweight object is created and held by associated objects (containers:...
    2 years ago
  • init-param vs context-param - see http://javahash.com/difference-between-servlet-init-and-context-parameter/ for background. Gist: context-param variables are global and accessible thro...
    2 years ago
  • rbenv vs RVM - RVM is responsible not only for changing Ruby versions, but for installing rubies and managing gemsets, as well. ...Along with rbenv [to manage ruby versi...
    2 years ago

Thursday, January 24, 2013

XML/Javascript Mapping with Jsonix

I needed a way to edit an XML config pulled from a SharePoint List (via SPServices). So that's when I found Jsonix, which so far in my testing appears to do the job quite well.

The steps I took were:
- Load the XML in Visual Studio and click XML, Create Schema. This created two .xsd files "MySettings.xsd" and "MySettings1.xsd" (in the same directory) as Visual Studio 2010 split them out by namespace apparently. At any rate, what I had to do was edit the xs:import element in the parent XSD, adding a schemaLocation attribute with the unqualified name of the second .xsd file.

- Downloaded jsonix-scripts-1.2-all.js and jsonix-full-1.2.jar   from http://repo2.maven.org/maven2/org/hisrc/jsonix/  -- note, I tried 1.0 per the how-to on the author's site, but it didn't seem to work for me (got errors which I didn't have the time to troubleshoot); however 1.2 worked like a charm, although the generated objects are rather cryptic.

- Ran C:\temp\jsonix-samples-po-1.0>java -jar lib/jsonix-full-1.2.jar -d src/main/webapp/js src/main/resources/MySettings.xsd
This generated two folders with a Mappings.js in each folder, corresponding to the two .xsd files. I combined these two .js files into one, putting the code from generated\Mappings.js at the top of the file and the code from settingsuri\Mappings.js (assuming "SettingsURI" is the root xmlns) at the bottom.

- I then uploaded my sample .xml and the .js files to the SharePoint list and placed the following code in a test .html page.


<script type="text/javascript" language="javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" language="javascript" src="jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript" language="javascript" src="jsonix-scripts-1.2-all.js"></script>
<script type="text/javascript" language="javascript" src="BothMappings.js"></script>

<script type="text/javascript" language="javascript">

    $(document).ready(function () {
 
var context = new Jsonix.Context([ settingsuri]);

var unmarshaller = context.createUnmarshaller();
var myURL = "MySettings.xml";
unmarshaller.unmarshalURL(myURL,
function (data) {
var objectFromURL = data;
alert(objectFromURL);
});

    });

</script>

- Turn on Debugging in IE Developer tools and place a breakpoint in the callback. The local watch clearly shows objectFromURL reflects the mappings of the XML file. Pretty sweet.

No comments:

Post a Comment