Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2013
    Rep Power

    Default convert xml to JSON using XPath dynamically

    Hi everybody!
    I'm trying to convert an xml file into JSON format. All i want to do is to loop through all the nodes of the xml file and for each specific node I want to loop through its attributes. I've written the following method :
    Java Code:
    public static void displayNodes(NodeList nl) throws JSONException {
    		for (int i = 0; i < nl.getLength(); i++) {
    			Node n = nl.item(i);
    			if (n.getNodeName()=="process"){
    				JSONWriter jw = new JSONStringer().array().object();
    					NamedNodeMap nnm = n.getAttributes();
    					        for (int j = 0; j < nnm.getLength(); j++){
    The output I get so far is:
    Exception in thread "main" org.json.JSONException: Misplaced key.
    at org.json.JSONWriter.key(
    at Main.displayNodes(
    at Main.main(

    but i want the result to be:
    {"id":"PROCESS_1", "isClosed":"false", "isExecutable":"true", "processType":"None" }]

    My xml file looks like this:
    Java Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <process id="PROCESS_1" isClosed="false" isExecutable="true" processType="None">
        <startEvent name="" text="Start Process" description="Start Process" note="null" id="20714" idbase="20714" stencil="WFD.Stencils.WF_START" category="NODE" modified="false" X="60" Y="60">
        <Task tasktype_id="158" text="Some text" description="Some description" note="Some note" orgunitgroupids="" rolesids="190|100.0|1.0|" documentsgroupids="" adequacy="100" compliance="100" responsibilitylevel_id="null" icon="314.png" layout="icon" uomtype_id="1" weight="11" uom_id="2" stateText="" isSelectedStateText="false" id="15282" idbase="15282" stencil="WFD.Stencils.WF_TASK" category="NODE" modified="false" X="180" Y="300" width="32" heigth="32">
        <endEvent id="_6" name="End Event">
    Last edited by blertadhimitri; 11-16-2013 at 10:08 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: convert xml to JSON using XPath dynamically

    My guess: shouldn't that line #13 be outside of that inner loop?

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Join Date
    Jan 2013
    Rep Power

    Default Re: convert xml to JSON using XPath dynamically

    Thanks Jos

    This really resolved my problem

Similar Threads

  1. How to convert xml to json using JSONWriter?
    By blertadhimitri in forum New To Java
    Replies: 3
    Last Post: 11-08-2013, 02:13 PM
  2. could not convert the xml to json using java
    By cgk_js in forum Advanced Java
    Replies: 0
    Last Post: 11-30-2012, 09:31 AM
  3. convert html to json
    By nigina in forum New To Java
    Replies: 3
    Last Post: 09-22-2011, 09:38 AM
  4. JSON Lib: json-lib-1.0-jdk13.jar
    By Raghuraman K in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 10:00 AM
  5. dynamically parse xpath predicates
    By Juuno in forum Advanced Java
    Replies: 1
    Last Post: 04-26-2009, 04:23 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts