Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to translate JSON to XML in Java
    primarykey
    data
    text
    <p>I wrote some Java code that is supposed to translate a JSON input file into an equivalent XML file. I used some answers from other questions on stackoverflow. However, I get an error:</p> <pre><code>[Fatal Error] :1:123: The markup in the document following the root element must be well-formed. Exception in thread "main" org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 123; The markup in the document following the root element must be well-formed. at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:251) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:300) at TranslateJSONtoXML.stringToDom(TranslateJSONtoXML.java:34) at TranslateJSONtoXML.main(TranslateJSONtoXML.java:43) </code></pre> <p>Why this error? I assume the JSON input is well-formed. So, here's the code:</p> <pre><code>import java.io.File; import java.io.IOException; import java.util.Scanner; import org.json.JSONObject; import org.json.XML; import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.*; public class TranslateJSONtoXML { public static String readFile(String pathname) throws IOException { File file = new File(pathname); StringBuilder fileContents = new StringBuilder((int)file.length()); Scanner scanner = new Scanner((Readable) new BufferedReader(new FileReader(file))); String lineSeparator = System.getProperty("line.separator"); try { while(scanner.hasNextLine()) { fileContents.append(scanner.nextLine()).append(lineSeparator); } return fileContents.toString(); } finally { scanner.close(); } } public static Document stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new InputSource(new StringReader(xmlSource))); } public static void main(String[] args) throws SAXException, ParserConfigurationException, IOException { String jsonString = readFile("/Users/monicamarcus/Downloads/Attachments_20131023/edl.json"); JSONObject o = new JSONObject(jsonString); String xml = org.json.XML.toString(o); System.out.println(xml); Document doc = stringToDom(xml); System.out.println(doc.getDocumentURI()); } } </code></pre> <p>The error comes from line return builder.parse(new InputSource(new StringReader(xmlSource)));</p> <p>Here's the XML I get. It is clearly not well formed, but I don't know why I get this:</p> <pre><code>&lt;javascript&gt;function textCounterEval(e,t){var n=t;var r=n.length;if(r&amp;gt;=e){return true}else{return false}}&lt;/javascript&gt; &lt;title&gt;Request Conversion&lt;/title&gt; &lt;configParams&gt;param1&lt;/configParams&gt; &lt;configParams&gt;param2&lt;/configParams&gt; &lt;instructions&gt;** Fields with an asterisk are required.&lt;/instructions&gt; &lt;name&gt;RequestJSON.eDoc.Form&lt;/name&gt; &lt;createInstructions&gt;** Fields with an asterisk are required.&lt;/createInstructions&gt; &lt;eventNotificationURL&gt;http://localhost:8080/xx-xxx/xxx.jsp&lt;/eventNotificationURL&gt; &lt;fieldDefs&gt; &lt;attributeName&gt;RequestChange.eDoc.isUrgent.RuleAttribute&lt;/attributeName&gt; &lt;title&gt;URGENT&lt;/title&gt; &lt;validation&gt;&lt;message&gt;Please Select a Campus.&lt;/message&gt; &lt;customValidator&gt;function addEvent(e,t,n,r){if(e.addEventListener){e.addEventListener(t,n,r);return true}else if(e.attachEvent){var i=e.attachEvent(&amp;apos;on&amp;apos;+t,n);return i}else{e[&amp;apos;on&amp;apos;+t]=n}}&lt;/customValidator&gt; &lt;regex&gt;[^Select]&lt;/regex&gt; &lt;required&gt;true&lt;/required&gt; &lt;/validation&gt;&lt;name&gt;isUrgent&lt;/name&gt; &lt;value&gt;no&lt;/value&gt; &lt;display&gt; &lt;values&gt; &lt;title&gt;--- Select ---&lt;/title&gt; &lt;/values&gt; &lt;values&gt; &lt;title&gt;Group1&lt;/title&gt; &lt;value&gt;sampleGroup1&lt;/value&gt; &lt;/values&gt; &lt;values&gt; &lt;title&gt;Group2&lt;/title&gt; &lt;value&gt;sampleGroup2&lt;/value&gt; &lt;/values&gt; &lt;valuesGroup&gt; &lt;values&gt; &lt;title&gt;NO&lt;/title&gt; &lt;value&gt;NO&lt;/value&gt; &lt;/values&gt; &lt;values&gt; &lt;title&gt;YES&lt;/title&gt; &lt;value&gt;YES&lt;/value&gt; &lt;/values&gt; &lt;dependsOn&gt; &lt;field&gt; &lt;name&gt;abc&lt;/name&gt; &lt;value&gt;XYZ&lt;/value&gt; &lt;/field&gt; &lt;/dependsOn&gt; &lt;/valuesGroup&gt; &lt;type&gt;select&lt;/type&gt; &lt;meta&gt; &lt;name&gt;cols&lt;/name&gt; &lt;value&gt;60&lt;/value&gt; &lt;/meta&gt; &lt;/display&gt; &lt;lookup&gt; &lt;fieldConversions&gt;conversions&lt;/fieldConversions&gt; &lt;businessObjectClassName&gt;org.xxx.xxxx&lt;/businessObjectClassName&gt; &lt;/lookup&gt; &lt;/fieldDefs&gt; &lt;fieldDefs&gt; &lt;title&gt;Date and Time for Change: MM/DD/YYYY&lt;/title&gt; &lt;validation&gt; &lt;message&gt;Enter a valid date in the format mm/dd/yyyy.&lt;/message&gt; &lt;regex&gt;^[0-1]?[0-9](/|-)[0-3]?[0-9](/|-)[1-2][0-9][0-9][0-9]$&lt;/regex&gt; &lt;required&gt;true&lt;/required&gt; &lt;/validation&gt; &lt;name&gt;dateOfChange&lt;/name&gt; &lt;display&gt; &lt;type&gt;text&lt;/type&gt; &lt;/display&gt; &lt;/fieldDefs&gt; &lt;fieldDefs&gt; &lt;title&gt;Description of the Change&lt;/title&gt; &lt;validation&gt; &lt;message&gt;Enter a description of the change.&lt;/message&gt; &lt;required&gt;true&lt;/required&gt; &lt;/validation&gt; &lt;name&gt;descriptionOfChange&lt;/name&gt; &lt;display&gt; &lt;type&gt;textarea&lt;/type&gt; &lt;meta&gt; &lt;name&gt;rows&lt;/name&gt; &lt;value&gt;5&lt;/value&gt; &lt;/meta&gt; &lt;meta&gt; &lt;name&gt;cols&lt;/name&gt; &lt;value&gt;60&lt;/value&gt; &lt;/meta&gt; &lt;/display&gt; &lt;/fieldDefs&gt;&lt; validations&gt; &lt;message&gt;If Request is OTHER, please enter Other Request.&lt;/message&gt; &lt;expression&gt;(wf:field(&amp;apos;reason&amp;apos;) = &amp;apos;Other&amp;apos; and not(wf:empty(wf:field(&amp;apos;otherDescription&amp;apos;)))) or (wf:field(&amp;apos;reason&amp;apos;) != &amp;apos;Other&amp;apos; and wf:empty(wf:field(&amp;apos;otherDescription&amp;apos;))) &lt;/expression&gt; &lt;/validations&gt; &lt;attributes&gt; &lt;name&gt;NetworkIdRole1Attribute&lt;/name&gt; &lt;fields&gt; &lt;attributeField&gt;user1Id&lt;/attributeField&gt; &lt;edlField&gt;RoleAdHoc&lt;/edlField&gt; &lt;/fields&gt; &lt;/attributes&gt; &lt;attributes&gt; &lt;name&gt;NetworkIdRole2Attribute&lt;/name&gt; &lt;fields&gt; &lt;attributeField&gt;user2Id&lt;/attributeField&gt; &lt;edlField&gt;RoleAdHoc&lt;/edlField&gt; &lt;/fields&gt; &lt;/attributes&gt; &lt;security&gt;Defines security for the edl document &lt;/security&gt; </code></pre> <p>I post also the JSON file. It looks well-formed, though.</p> <pre><code>{ "name": "RequestJSON.eDoc.Form", "title": "Request Conversion", "configParams": [ "param1", "param2" ], "security": "Defines security for the edl document", "eventNotificationURL": "http://localhost:8080/xx-xxx/xxx.jsp", "createInstructions": "** Fields with an asterisk are required.", "instructions": "** Fields with an asterisk are required.", "validations": [ { "expression": "(wf:field('reason') = 'Other' and not(wf:empty(wf:field('otherDescription')))) or (wf:field('reason') != 'Other' and wf:empty(wf:field('otherDescription')))", "message": "If Request is OTHER, please enter Other Request." } ], "attributes": [ { "name": "NetworkIdRole1Attribute", "fields": [ { "attributeField": "user1Id", "edlField": "RoleAdHoc" } ] }, { "name": "NetworkIdRole2Attribute", "fields": [ { "attributeField": "user2Id", "edlField": "RoleAdHoc" } ] } ], "javascript": "function textCounterEval(e,t){var n=t;var r=n.length;if(r&gt;=e){return true}else{return false}}", "fieldDefs": [ { "attributeName": "RequestChange.eDoc.isUrgent.RuleAttribute", "name": "isUrgent", "title": "URGENT", "value": "no", "display": { "type": "select", "meta": [ { "name": "cols", "value": "60" } ], "values": [ { "title": "--- Select ---" }, { "title": "Group1", "value": "sampleGroup1" }, { "title": "Group2", "value": "sampleGroup2" } ], "valuesGroup": { "dependsOn": { "field": { "name": "abc", "value": "XYZ" } }, "values": [ { "title": "NO", "value": "NO" }, { "title": "YES", "value": "YES" } ] } }, "lookup": { "businessObjectClassName": "org.xxx.xxxx", "fieldConversions": "conversions" }, "validation": { "required": "true", "regex": "[^Select]", "customValidator": "function addEvent(e,t,n,r){if(e.addEventListener){e.addEventListener(t,n,r);return true}else if(e.attachEvent){var i=e.attachEvent('on'+t,n);return i}else{e['on'+t]=n}}", "message": "Please Select a Campus." } }, { "name": "dateOfChange", "title": "Date and Time for Change: MM/DD/YYYY", "display": { "type": "text" }, "validation": { "required": "true", "regex": "^[0-1]?[0-9](/|-)[0-3]?[0-9](/|-)[1-2][0-9][0-9][0-9]$", "message": "Enter a valid date in the format mm/dd/yyyy." } }, { "name": "descriptionOfChange", "title": "Description of the Change", "display": { "type": "textarea", "meta": [ { "name": "rows", "value": "5" }, { "name": "cols", "value": "60" } ] }, "validation": { "required": "true", "message": "Enter a description of the change." } } ] } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload