I am trying to render a json that contains escaped xml. So " should be \", new lines \n, etc. To make it more readable I want to divide that into two files, one with json, the second one with xml. Both of them must be templates as they have some dynamic values.
{
"xml" : "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n ..."
}
json.ftl:
{
"xml" : "<#include "xml.ftl">"
}
xml.ftl:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>...
How can I achieve that escaping? I know that there are js_string and json_string but the problem is that I do not know how to apply them with include. Thanks for help
You could make a macro like this:
<#macro includeAsJsonString templateName>
<#local captured><#include templateName></#local>
${captured?json_string}<#t>
</#macro>
and then you do this:
{
"xml" : "<#includeAsJsonString 'xml.ftl' />"
}
(Of course, you don't have to create a macro for this, but I think that's more reusable.)
Related
I have a Json which contains another Json as a string. I am already able to read the inner Json and getting its contents.
My Json currently looks like this:
[
{
"fullName": "FullNameTest",
"dataType": 3,
"configuration": "[{\"IPAddress\": \"123.123.123\",\"Node\": \"my'node'path'is'here'\"}]"
}
]
As you can see, the second Json is inside of "configuration". The second parameter called "Node" contains a node path which is currently seperated by " ' ". The problem is that I need to replace this with quotation marks because my code will not work otherwise. I also cannot do anything against that.
I am currently replacing the " ' " with some code but I don't think that is a pretty solution.
You can escape it twice, like this:
\\\"text\\\"
I have XML documents that I am trying to convert to Json but some of the string fields have HTML tags in them (from copy/paste of text fields from Word documents). The source XML looks like this:
<my:Request_Description>
<html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
<div>test</div>
</html>
</my:Request_Description>
When JsonConvert.SerializeXmlNode is called the Json ends up as this:
"Request_Description": {
"html": {
"#xml:space": "preserve",
"#xmlns": "http://www.w3.org/1999/xhtml",
"#significant-whitespace": [
"\r\n ",
"\r\n"
],
"div": "test"
}
}
I tried to just declare the field as a string but when calling deserializeobject the error is Unexpected character encountered while parsing value.
Is there something I should do on the serializexmlnode to make the Json result different? Or is there something I can do on the deserializeobject to have it ignore the HTML tag?
Ideally the json would be something like below but I assume some escape characters would need to be included for the quote marks. The main point being that HTML tags do NOT denote a separate node but instead are part of the value for the node. I started looking into XSLT and thought that might be an option.
{
"Request_Description": "<html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml"><div>test</div></html>"
}
Switched to using XDocument and this code worked.
XElement req_desc = newxdoc.Root.Element("Request_Description");
if (req_desc != null)
{
XElement replacenode = new XElement(req_desc.Name, req_desc.Value);
req_desc.Parent.Add(replacenode);
req_desc.Remove();
}
I just wanted to know if I can do something like this in JSON:
{
"web" : {
"app_pub" : "localhost/public",
"app_lib" : "localhost/lib",
"app_assets" : "app_pub" + "/assets"
}
}
You can check your JSON, using validator, i.e.:
https://jsonformatter.curiousconcept.com/
Your code doesn't correspond RFC4627 and others.
You can't do operations inside JSON.
P.S.:
RFC4627 contains full JSON grammar.
This line:
Dim NewHTMLString As String = XDocument.Parse(htmldoc.DocumentNode.OuterHtml).ToString()
Produces this error:
The '"' character, hexadecimal value 0x22, cannot be included in a name.
This is the line in the HTML it says is wrong:
if ( typeof JSON != 'object' || !JSON.stringify || !JSON.parse ) { document.write( "<scr" + "ipt type=\"text\/javascript\" src=\"http:\/\/blahblah"><\/script>\n" ); };
That's because XDocument meant to deal with XML, hence it doesn't support arbitrary Javascript string. XDocument thinks this part : <scr", as beginning of an XML node and double-quote (") character in the XML node name is considered invalid.
I was using XDocument in the answer to your previous question to get beautifully formatted XML output in console, and I did that because I know exactly that my HTML is XML compliant. In this case, your HTML isn't valid from XML point of view and it isn't clear what you're trying to achieve using XDocument here. If you simply need to check result from modification you did to the original HTML, you can either directly print htmldoc.DocumentNode.OuterHtml to console or save the HTML to a new file like so :
htmldoc.Save("path_to_new_file.html")
I am working on eXist database, I am have a new Idea that I have to implement XML file using XQuery.
I want to convert CSV file to XML which is already in the database collection. And this XML file contains only necessary tags and information. And this converted data will be saved into XML in eXist Database.
XML like this: its name is 'createXML.xml'
<?xml version="1.0" encoding="UTF-8"?>
<records>
// All the Records from CSV file want to put here..... between Records tags
</records>
now the CSV file like this:
name,subject,marks //header lines
krunal,maths,95
abc,sub1,87
def,sub2,67
...
Output like this
<?xml version="1.0" encoding="UTF-8"?>
<records>
<user>
<name>krunal</name>
<subject>maths</subject>
<marks>95</marks>
</user>
<user>
<name>abc</name>
<subject>sub1</subject>
<marks>87</marks>
</user>
<user>
<name>def</name>
<subject>sub2</subject>
<marks>67</marks>
</user>
.
.
.
</records>
Can anyone provide me how to add CSV data to already available XML using XQuery in eXist database and performs this function.
For reading a file look at your XQuery implementations documentations, here reading a file for eXist.
Wikibooks has an excellent example on how to parse CSV:
let $csv := 'name,faculty
alice,anthropology
bob,biology'
let $lines := tokenize($csv, '\n')
let $head := tokenize($lines[1], ',')
let $body := remove($lines, 1)
return
<people>
{
for $line in $body
let $fields := tokenize($line, ',')
return
<person>
{
for $key at $pos in $head
let $value := $fields[$pos]
return
element { $key } { $value }
}
</person>
}
</people>
Another possibility would be to use another XQuery engine with builtin csv import support like Zorba or BaseX.
Assuming (1) that your CSV file is already in the database and (2) that you want to store the result of the CSV-to-XML in a new XML file, then there are 3 parts to your XQuery:
Get the contents of the CSV file using util:binary-doc() and
util:binary-to-string(). Note that util:binary-to-string() assumes the file is encoded with UTF-8, but as the documentation shows you can tell the function the encoding scheme if your CSV file has a non-UTF-8 encoding.
Transform the CSV to your desired structure, using the XQuery Wikibook article on parsing CSV (already mentioned in #Ranon's reply). I assume you know enough XQuery to adapt this routine into your own function — which I will call local:csv-to-xml() below.
Store the resulting XML in the database using use the xmldb:store() function
(Note that the util and xmldb module are eXist-db specific modules, as this functionality is not covered by the XQuery specification. If you use a different XQuery implementation, you will need to use their implementation-specific methods.)
So here is the eXist-db solution:
let $csv-file := '/db/myCSV.csv'
let $csv-binary := util:binary-doc($csv-file)
let $csv := util:binary-to-string($csv-binary)
let $xml := local:csv-to-xml($csv)
return
xmldb:store('/db', 'createXML.xml', $xml)
This will store the converted version of myCSV.csv as createXML.xml at the root of your eXist database: /db/createXML.xml.
If, instead, you wanted to append the XML result to an existing XML file, you will want to apply XQuery Update syntax — which lets you insert nodes, replace nodes, and delete nodes in documents stored in the database. In eXist-db, the documentation for eXist's XQuery Update syntax is at http://exist-db.org/exist/update_ext.xml.