Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   convertir json a xml (https://www.clubdelphi.com/foros/showthread.php?t=94045)

ingabraham 04-07-2019 18:08:08

convertir json a xml
 
buenas necesito convertir una estructura json a xml.
gracias
ejemplo:
Código:

{
  "email": {
    "mensaje": {
      "remite": {
        "nombre": "Alfredo Reino",
        "email": "alf@ibium.com"
      },
      "destinatario": {
        "nombre": "Bill Clinton",
        "email": "president@whitehouse.gov"
      },
      "asunto": "Hola Bill",
      "texto": {
        "parrafo": {
          "#text": [
            "¿Hola qué tal? Hace ",
            " que no escribes. A ver si llamas y quedamos para tomar algo."
          ],
          "enfasis": "mucho"
        }
      }
    }
  }
}

convertido a:


Código:

<?xml version="1.0" encoding="UTF-8" ?>
        <email>
                <mensaje>
                        <remite>
                                <nombre>Alfredo Reino</nombre>
                                <email>alf@ibium.com</email>
                        </remite>
                        <destinatario>
                                <nombre>Bill Clinton</nombre>
                                <email>president@whitehouse.gov</email>
                        </destinatario>
                        <asunto>Hola Bill</asunto>
                        <texto>
                                <parrafo>
                                        ¿Hola qué tal? Hace  que no escribes. A ver si llamas y quedamos para tomar algo.<enfasis>mucho</enfasis>
                                </parrafo>
                        </texto>
                </mensaje>
        </email>


Casimiro Notevi 04-07-2019 19:15:53

Con delphi:
https://torry.net/quicksearchd.php?S...+xml&Title=Yes

Con opciones online:
http://www.utilities-online.info/xmltojson/
http://convertjson.com/json-to-xml.htm
https://www.freeformatter.com/json-t...converter.html
https://jsonformatter.org/json-to-xml

ingabraham 04-07-2019 21:13:01

gracias amigo. pero es para realizar las funciones en delphi. no externo a delphi
muchas gracias

ingabraham 05-07-2019 01:08:23

por ejemplo recorrer un json e ir armando un xml en delphi

Casimiro Notevi 05-07-2019 09:49:36

Creo que no has leído mi respuesta anterior :)

ingabraham 05-07-2019 16:44:52

Cita:

Empezado por Casimiro Notevi (Mensaje 532639)
Creo que no has leído mi respuesta anterior :)

gracias por tu atencion,. amigo solo baja un ejecutable. no esta el codigo fuente delphi!

Casimiro Notevi 05-07-2019 17:34:19

Habría jurado que estaba :confused:
No sé si con los últimos delphi se puede.
Mira esto, a ver si te sirve, no sé en qué lenguaje está.

ingabraham 05-07-2019 17:37:11

ENCONTRE ESTA funcion que recorre todo el json , con sus array.
en una unit llamada jsontreeview .
tocaria ir armando el xml o algo asi. si tienen ideas


Código Delphi [-]
procedure TJSONTreeView.LoadJson;
var v: TJSONValue; currNode: TTreeNode; i, aCount: integer; s: string;
begin
  ClearAll;

  if (JSONDocument <> nil) and JSONDocument.IsActive then
  begin
    v := JSONDocument.RootValue;
    Items.Clear;

    if TJSONDocument.IsSimpleJsonValue(v) then
      Items.AddChild(nil, TJSONDocument.UnQuote(v.Value))

    else
    if v is TJSONObject then
    begin
      aCount := TJSONObject(v).Size;
      s := '{}';
      if VisibleChildrenCounts then
        s := s + ' (' + IntToStr(aCount) + ')';
      if VisibleByteSizes then
        s := s + ' (size: ' + IntToStr(v.EstimatedByteSize) + ' bytes)';
      currNode := Items.AddChild(nil, s);
      for i := 0 to aCount - 1 do
        ProcessPair(currNode, TJSONObject(v), i)
    end

    else
    if v is TJSONArray then
    begin
      aCount := TJSONArray(v).Size;
      s := '[]';
      if VisibleChildrenCounts then
        s := s + ' (' + IntToStr(aCount) + ')';
      if VisibleByteSizes then
        s := s + ' (size: ' + IntToStr(v.EstimatedByteSize) + ' bytes)';
      currNode := Items.AddChild(nil, s);
      for i := 0 to aCount - 1 do
        ProcessElement(currNode, TJSONArray(v), i)
    end

    else
      raise EUnknownJsonValueDescendant.Create;

    FullExpand;
  end;
end;

procedure TJSONTreeView.ProcessPair(currNode: TTreeNode; obj: TJSONObject; aIndex: integer);
var p: TJSONPair; s: string; n: TTreeNode; i, aCount: integer;
begin
  p := obj.Get(aIndex);

  s := TJSONDocument.UnQuote(p.JsonString.ToString) + ' : ';

  if TJSONDocument.IsSimpleJsonValue(p.JsonValue) then
  begin
    Items.AddChild(currNode, s + p.JsonValue.ToString);
    exit;
  end;

  if p.JsonValue is TJSONObject then
  begin
    aCount := TJSONObject(p.JsonValue).Size;
    s := s + ' {}';
    if VisibleChildrenCounts then
      s := s + ' (' + IntToStr(aCount) + ')';
    if VisibleByteSizes then
        s := s + ' (size: ' + IntToStr(p.EstimatedByteSize) + ' bytes)';
    n := Items.AddChild(currNode, s);
    for i := 0 to aCount - 1 do
      ProcessPair(n, TJSONObject(p.JsonValue), i);
  end

  else if p.JsonValue is TJSONArray then
  begin
    aCount := TJSONArray(p.JsonValue).Size;
    s := s + ' []';
    if VisibleChildrenCounts then
      s := s + ' (' + IntToStr(aCount) + ')';
    if VisibleByteSizes then
        s := s + ' (size: ' + IntToStr(p.EstimatedByteSize) + ' bytes)';
    n := Items.AddChild(currNode, s);
    for i := 0 to aCount - 1 do
      ProcessElement(n, TJSONArray(p.JsonValue), i);
  end
  else
    raise EUnknownJsonValueDescendant.Create;
end;

procedure TJSONTreeView.ProcessElement(currNode: TTreeNode; arr: TJSONArray; aIndex: integer);
var v: TJSONValue; s: string; n: TTreeNode; i, aCount: integer;
begin
  v := arr.Get(aIndex);
  s := '[' + IntToStr(aIndex) + '] ';

  if TJSONDocument.IsSimpleJsonValue(v) then
  begin
    Items.AddChild(currNode, s + v.ToString);
    exit;
  end;

  if v is TJSONObject then
  begin
    aCount := TJSONObject(v).Size;
    s := s + ' {}';
    if VisibleChildrenCounts then
      s := s + ' (' + IntToStr(aCount) + ')';
    if VisibleByteSizes then
        s := s + ' (size: ' + IntToStr(v.EstimatedByteSize) + ' bytes)';
    n := Items.AddChild(currNode, s);
    for i := 0 to aCount - 1 do
      ProcessPair(n, TJSONObject(v), i);
  end

  else if v is TJSONArray then
  begin
    aCount := TJSONArray(v).Size;
    s := s + ' []';
    n := Items.AddChild(currNode, s);
    if VisibleChildrenCounts then
      s := s + ' (' + IntToStr(aCount) + ')';
    if VisibleByteSizes then
        s := s + ' (size: ' + IntToStr(v.EstimatedByteSize) + ' bytes)';
    for i := 0 to aCount - 1 do
      ProcessElement(n, TJSONArray(v), i);
  end
  else
    raise EUnknownJsonValueDescendant.Create;

end;


La franja horaria es GMT +2. Ahora son las 00:37:56.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi