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

    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();
    				if(n.hasAttributes()){
    					NamedNodeMap nnm = n.getAttributes();
    					        for (int j = 0; j < nnm.getLength(); j++){
    						
    						jw.key(nnm.item(j).getNodeName().toString()).value(nnm.item(j).getNodeValue());
    						jw.endObject().endArray();
    						
    						System.out.println(jw.toString());
    					
    				}
    				
    			}
    The output I get so far is:
    [{"id":"PROCESS_1"}]
    Exception in thread "main" org.json.JSONException: Misplaced key.
    at org.json.JSONWriter.key(JSONWriter.java:220)
    at Main.displayNodes(Main.java:42)
    at Main.main(Main.java:18)


    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"?>
    <definitions>
      <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">
        </startEvent>
        <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">
        </Task>
        <endEvent id="_6" name="End Event">
        </endEvent>
      </process>
    </definitions>
    Last edited by blertadhimitri; 11-16-2013 at 10:08 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: convert xml to JSON using XPath dynamically

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Jan 2013
    Posts
    64
    Rep Power
    0

    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
  •