Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 9 of 9 total
Thread XML
Sat, May 23 2015 2:29 PMPermanent Link

Uli Becker

Hi,

I've to parse XML strings and would like to use TReader for that. Is
there a simple way to convert XML to JSON, maybe by using an external JS?

Thanks Uli
Tue, May 26 2015 4:08 AMPermanent Link

Matthew Jones

Uli Becker wrote:

> Hi,
>
> I've to parse XML strings and would like to use TReader for that. Is
> there a simple way to convert XML to JSON, maybe by using an external
> JS?

Not sure it is particularly easy, but XML itself isn't hard. I think
this is built into the browser, but this is a snippet from code that I
had working well:

procedure TfrmClicker.ParseQuestionXML(szXML : String);
var
   respDoc: TDocument;
   xDataNodeList : TNodeList;
   // ..
begin
   try
   respDoc := getDOMfromXMLstring(szXML);

   xDataNodeList := respDoc.getElementsByTagName('Anonymous');
   if xDataNodeList.Length > 0 then
   begin
       szAnonymousMode := xDataNodeList[0].firstChild.nodeValue;
   end;

   szKind := '1 to n';
   xDataNodeList := respDoc.getElementsByTagName('Kind');
   if xDataNodeList.Length > 0 then
   begin
       szKind := xDataNodeList[0].firstChild.nodeValue;
   end;

  xDataNodeList := respDoc.getElementsByTagName('Title');
  if xDataNodeList.Length > 0 then
  begin
      pnlMain.Caption := frmJoin123.ApplicationTitle + ': "' +
xDataNodeList[0].firstChild.nodeValue + '"';
  end;
Tue, May 26 2015 9:29 AMPermanent Link

Uli Becker

Matthew,

as I wrote I want to parse a JSON string with TReader. That's why I need to convert the xml string.
I found some scripts, but none of them produced a clean JSON string.

Thanks

Uli
Tue, May 26 2015 10:55 AMPermanent Link

Matthew Jones

Uli Becker wrote:

> as I wrote I want to parse a JSON string with TReader. That's why I
> need to convert the xml string.  I found some scripts, but none of
> them produced a clean JSON string.

Can it actually be done? JSON is not quite so rich as XML which has
attributes, and can have content separate to the nodes.

Anyway, I figured you could parse what you want and make the JSON, but
obviously you know your needs better.

(A quick Google shows things like
http://www.freeformatter.com/xml-to-json-converter.html which has
various limitations)

Indeed, https://code.google.com/p/x2js/ is a JavaScript thing you could
probably link in?
Tue, May 26 2015 11:45 AMPermanent Link

Raul

Team Elevate Team Elevate

On 5/23/2015 2:29 PM, Uli Becker wrote:
> I've to parse XML strings and would like to use TReader for that. Is
> there a simple way to convert XML to JSON, maybe by using an external JS?

Matthew provided some links but the main issues i can see are use of
attributes and figuring out data types (unless you want to handle
namespaces and rest of the XML craziness).

If XML is straightforward then writing a basic converter in EWB would be
easy (especially if you go with string as value everywhere) - though
handling any random XML gets trickier.

Raul
Tue, May 26 2015 11:56 AMPermanent Link

Matthew Jones

Raul wrote:

>  figuring out data types

Good point, though IIRC some interface code of mine looks at the data
type and does the necessary, so if you want an Integer and it is
actually a string then it does the conversion. That isn't a general
solution though, particularly if the JSON is going to be used for
native object streaming.
Tue, May 26 2015 12:14 PMPermanent Link

Raul

Team Elevate Team Elevate

On 5/26/2015 11:56 AM, Matthew Jones wrote:
> Good point, though IIRC some interface code of mine looks at the data
> type and does the necessary, so if you want an Integer and it is
> actually a string then it does the conversion. That isn't a general
> solution though, particularly if the JSON is going to be used for
> native object streaming.

This would definitely mean your parser has to be content aware.

Doing XML schema parsing is PITA though. If you can control XML
generation then forcing type into an attribute would be one way to go.

<name type="xs:string">John</name>

this would be limited to specific XML sceanrios but at least generic
(i'm not actually sure this would pass a xml validation but would be
darn easy to parse with EWB).

Raul
Tue, May 26 2015 2:15 PMPermanent Link

Uli Becker

Raul,

> Matthew provided some links but the main issues i can see are use of
> attributes and figuring out data types (unless you want to handle
> namespaces and rest of the XML craziness).

I know these links because I have been googling for quite a long time. Smile
And I tried them all... but as I said: none of them produced a clean
JSON. Maybe it's just not possible.

> If XML is straightforward then writing a basic converter in EWB would be
> easy (especially if you go with string as value everywhere) - though
> handling any random XML gets trickier.

When I've some time left, I'll look into this, thanks.

Uli
Wed, May 27 2015 5:57 PMPermanent Link

Raul

Team Elevate Team Elevate

On 5/26/2015 2:15 PM, Uli Becker wrote:
> I know these links because I have been googling for quite a long time. Smile
> And I tried them all... but as I said: none of them produced a clean
> JSON. Maybe it's just not possible.
>
> When I've some time left, I'll look into this, thanks.

One more thing

I was just working on some XML and realized arrays are another issue
that is really trivial in JSON (basically just comma separated) but how
should XML look like.

One i have uses this structure:
....
<customer> ... </customer>
<customer> ... </customer>
<customer> ... </customer>
....

which is fine but of course but now the converter has to detect these
identical consecutive tags and assemble into array for JSON.

Raul
Image