IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Web & réseau Delphi Discussion :

Impossible de trouver un noeud existant en XML


Sujet :

Web & réseau Delphi

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    68
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 68
    Points : 46
    Points
    46
    Par défaut Impossible de trouver un noeud existant en XML
    Bonjour,
    Je continue mon exploration laborieuse de XML.
    J'essaie de chercher un noeud existant dans ce XML:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    <?xml version="1.0"?>
    <root xmlns="urn:schemas-upnp-org:device-1-0">
    	<specVersion>
    		<major>1</major>
    		<minor>0</minor>
    	</specVersion>
    	<device>
    		<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>
    		<friendlyName>ILCE-7S</friendlyName>
    		<manufacturer>Sony Corporation</manufacturer>
    		<manufacturerURL>http://www.sony.net/</manufacturerURL>
    		<modelDescription>SonyDigitalMediaServer</modelDescription>
    		<modelName>SonyImagingDevice</modelName>
    		<UDN>uuid:000000001000-1010-8000-7A4B879B63A0</UDN>
    		<serviceList>
    			<service>
    				<serviceType>urn:schemas-sony-com:service:ScalarWebAPI:1</serviceType>
    				<serviceId>urn:schemas-sony-com:serviceId:ScalarWebAPI</serviceId>
    				<SCPDURL/>
    				<controlURL/>
    				<eventSubURL/>
    			</service>
    			<service>
    				<serviceType>urn:schemas-sony-com:service:DigitalImaging:1</serviceType>
    				<serviceId>urn:schemas-sony-com:serviceId:DigitalImaging</serviceId>
    				<SCPDURL>/DigitalImagingDesc.xml</SCPDURL>
    				<controlURL>/upnp/control/DigitalImaging</controlURL>
    				<eventSubURL></eventSubURL>
    			</service>
    		</serviceList>
    		<av:X_ScalarWebAPI_DeviceInfo xmlns:av="urn:schemas-sony-com:av">
    			<av:X_ScalarWebAPI_Version>1.0</av:X_ScalarWebAPI_Version>
    			<av:X_ScalarWebAPI_ServiceList>
    				<av:X_ScalarWebAPI_Service>
    					<av:X_ScalarWebAPI_ServiceType>guide</av:X_ScalarWebAPI_ServiceType>
    					<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
    					<av:X_ScalarWebAPI_AccessType/>
    				</av:X_ScalarWebAPI_Service>
    				<av:X_ScalarWebAPI_Service>
    					<av:X_ScalarWebAPI_ServiceType>accessControl</av:X_ScalarWebAPI_ServiceType>
    					<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
    					<av:X_ScalarWebAPI_AccessType/>
    				</av:X_ScalarWebAPI_Service>
    				<av:X_ScalarWebAPI_Service>
    					<av:X_ScalarWebAPI_ServiceType>camera</av:X_ScalarWebAPI_ServiceType>
    					<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
    					<av:X_ScalarWebAPI_AccessType/>
    				</av:X_ScalarWebAPI_Service>
    				<av:X_ScalarWebAPI_Service>
    					<av:X_ScalarWebAPI_ServiceType>avContent</av:X_ScalarWebAPI_ServiceType>
    					<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
    					<av:X_ScalarWebAPI_AccessType/>
    				</av:X_ScalarWebAPI_Service>
    			</av:X_ScalarWebAPI_ServiceList>
    		</av:X_ScalarWebAPI_DeviceInfo>
    	</device>
    </root>
    Mon programme affiche tous les noeuds enfants de 'device', j'obtiens la liste suivante:
    deviceType
    friendlyName
    manufacturer
    manufacturerURL
    modelDescription
    modelName
    UDN
    serviceList
    av:X_ScalarWebAPI_DeviceInfo
    Jusque la c'est normal, quand je recherche le noeud 'UDN' pas de soucis, par contre je n'arrive jamais à trouver le noeud 'av:X_ScalarWebAPI_DeviceInfo'. Voici le code qui fait cela:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    procedure TForm1.ButtonTestXMLClick(Sender: TObject);
    var
      f:TextFile;
      Recherche:String;
      DeviceNode,ServiceListNode : IXMLNode;
      ANode : IXMLNode;
      STitle: WideString;
      i:integer;
    Begin
      XMLDoc.LoadFromFile('D:\Astro\Sony\XML.XML');
      XMLDoc.Active:=True;
      if XMLDoc.DocumentElement <> nil then
          memo1.Lines.Assign(XMLDoc.XML);
      XMLDoc.Active:=True;
      DeviceNode := XMLDoc.DocumentElement.ChildNodes.FindNode('device') ;
      for i:=0 to DeviceNode.ChildNodes.Count-1 do
      begin
        memo1.Lines.Add(DeviceNode.ChildNodes[i].NodeName); // j'affiche tous les sous noeuds de 'device'
        Recherche:=DeviceNode.ChildNodes[i].NodeName;
      end;
      ServiceListNode := DeviceNode .ChildNodes.FindNode('UDN');  //pas de soucis le noeud est trouvé
      ServiceListNode := DeviceNode .ChildNodes.FindNode('av:X_ScalarWebAPI_Service'); //IMPOSSIBLE à trouver!!
    //  ANode := ServiceListNode;
    //  STitle:=ANode.Text;
    end;
    Merci de vos avis éclairés.

  2. #2
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 021
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 021
    Points : 40 935
    Points
    40 935
    Billets dans le blog
    62
    Par défaut
    Bonjour,

    j'ai eu le même problème avec ce type d'attribut.
    je suggère

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    aNameSpace : String;
    ...
    aNameSpace:=DeviceNode.FindNamespaceURI('av');
    ServiceListNode:=DeviceNode.FindNode('X_ScalarWebAPI_Service',aNameSpace);
    Quoique j'ai un doute sur le DeviceNode dans aNameSpace:=
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  3. #3
    Expert éminent sénior
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Points : 28 430
    Points
    28 430
    Par défaut
    pour le XML je n'utilise plus que mon XMLTree, voir la fonction test() pour des exemples d'usage.
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    68
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 68
    Points : 46
    Points
    46
    Par défaut
    Bonjour,
    Merci de l'aide mais le :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
      Recherche:=DeviceNode.FindNamespaceURI('av');
    me renvoie une chaine vide, bon en bref ça ne marche pas. Ca me gonfle le XML!!!

    Michel

  5. #5
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 021
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 021
    Points : 40 935
    Points
    40 935
    Billets dans le blog
    62
    Par défaut
    Moui, bon, il faut persévérer

    Déjà X_ScalarWebAPI_Service n'est pas un noeud de 'Device' mais un noeud de 'X_ScalarWebAPI_DeviceInfo'

    Donc pour lister :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    procedure TForm54.Button1Click(Sender: TObject);
    var
      f:TextFile;
      Recherche:String;
      DeviceNode,ServiceListNode,aSWebService : IXMLNode;
      ANode : IXMLNode;
      STitle: WideString;
      i:integer;
     
    const av = 'urn:schemas-sony-com:av';
     
    Begin
     
    //  DefaultDOMVendor := 'ADOM XML v4';
      XMLDoc.LoadFromFile('Test.XML');
      XMLDoc.Active:=True;
     
      DeviceNode := XMLDoc.DocumentElement.ChildNodes.FindNode('device') ;
      for i:=0 to DeviceNode.ChildNodes.Count-1 do
      begin
        memo1.Lines.Add(DeviceNode.ChildNodes[i].NodeName); // j'affiche tous les sous noeuds de 'device'
        Recherche:=DeviceNode.ChildNodes[i].NodeName;
      end;
      ServiceListNode := DeviceNode.ChildNodes.FindNode('UDN');  //pas de soucis le noeud est trouvé
      ServiceListNode := DeviceNode.ChildNodes.FindNode('serviceList');
      ServiceListNode := DeviceNode.ChildNodes.FindNode('X_ScalarWebAPI_DeviceInfo',av);   // Attention je passe en av: 
      for I := 0 to ServiceListNode.ChildNodes.Count-1 do
        memo1.lines.add('--- '+ServiceListNode.ChildNodes[i].NodeName) ; 
     // pour atteindre X_ScalarWebAPI_ServiceList 
     aSWebService:=ServiceListNode.ChildNodes.FindNode('X_ScalarWebAPI_ServiceList');  
    // liste les types
      for I := 0 to aSWebService.ChildNodes.Count-1 do
        begin
          Memo1.Lines.Add(aSWebService.ChildNodes[i].ChildNodes.FindNode('X_ScalarWebAPI_ServiceType',av).NodeValue);
        end;
    end;
    NB : const av = 'urn:schemas-sony-com:av'; // j'en ai fait une constante mais j'obtenais la même chose en utilisant ce code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
      DeviceNode := XMLDoc.DocumentElement.ChildNodes.FindNode('device') ;
      for i:=0 to DeviceNode.ChildNodes.Count-1 do
      begin
        memo1.Lines.Add(DeviceNode.ChildNodes[i].NodeName); // j'affiche tous les sous noeuds de 'device'
        if DeviceNode.ChildNodes[i].NodeName.StartsWith('av:') then Recherche:=DeviceNode.ChildNodes[i].NodeName;
      end;
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  6. #6
    Expert éminent sénior
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Points : 28 430
    Points
    28 430
    Par défaut
    Citation Envoyé par michastro Voir le message
    Bonjour,
    Merci de l'aide mais le :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
      Recherche:=DeviceNode.FindNamespaceURI('av');
    me renvoie une chaine vide, bon en bref ça ne marche pas. Ca me gonfle le XML!!!

    Michel
    en vrai j'ai une autre approche pour ce genre de XML de taille réduire.. Index := Pos('av:X_ScalarWebAPI_DeviceInfo', xml)

    ou encore mon unité FastXML

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
     
    unit Execute.FastXML;
     
    // Quick & Dirty XML parser (c)2019 Execute SARL <contact@execute.fr>
     
    interface
     
    uses
      System.SysUtils;
     
    type
      TFastXML = record
        Text: string;
        function LoadFromFile(const AFileName: string): Boolean;
        function LoadSection(const AName: string): Boolean;
        function GetSection(const AName: string; var AIndex: Integer; var Content: string): Boolean;
        function GetString(const AName: string; ADefault: string = ''): string;
        function GetInt(const AName: string; ADefault: Integer): Integer;
        function GetBool(const AName: string; ADefault: Boolean): Boolean;
        function GetAttribute(const ANode, AName: string): string;
      end;
     
    implementation
     
    function TFastXML.LoadFromFile(const AFileName: string): Boolean;
    var
      Bytes: TBytes;
      F    : file;
      Size : Integer;
    begin
      Text := '';
      IoResult; // Clear IoREsult
      AssignFile(F, AFileName);
      try
        Reset(F, 1);
        if IoResult <> 0 then
          Exit(False);
        try
          Size := FileSize(F);
          if Size <= 0 then
            Exit(False);
          SetLength(Bytes, Size);
          BlockRead(F, Bytes[0], Size);
        finally
          CloseFile(F);
        end;
        Text := TEncoding.UTF8.GetString(Bytes);
        Result := True;
      except
        Result := False;
      end;
    end;
     
    function TFastXML.LoadSection(const AName: string): Boolean;
    var
      Index: Integer;
      Section: string;
    begin
      Index := 1;
      Result := GetSection(AName, Index, Section);
      if Result then
        Text := Section
      else
        Text := '';
    end;
     
    function TFastXML.GetSection(const AName: string; var AIndex: Integer; var Content: string): Boolean;
    var
      Start, Stop, Opens, Closes, Index: Integer;
    begin
      Result := False;
      Start := Pos('<' + AName, Text, AIndex);
      if Start = 0 then
        Exit;
      Closes := 0;
      Stop := Pos('>', Text, Start);
      if Stop = 0 then
        Exit;
      if Text[Stop - 1] = '/' then
      begin
        Inc(Stop);
        Content := Copy(Text, Start, Stop - Start);
      end else begin
        repeat
          Stop := Pos('</' + AName + '>', Text, Stop);
          if Stop = 0 then
            Exit;
          Inc(Stop, 3 + Length(AName));
          Inc(Closes);
          Content := Copy(Text, Start, Stop - Start);
          Opens := 0;
          Index := 1;
          repeat
            Inc(Opens);
            Index := Pos('<' + AName, Content, Index + 1);
          until Index = 0;
        until Opens = Closes;
      end;
      Result := True;
      AIndex := Stop;
    end;
     
    function TFastXML.GetString(const AName: string; ADefault: string = ''): string;
    var
      Start, Stop: Integer;
    begin
      Start := Pos(AName, Text);
      while Start > 0 do
      begin
        if (Text[Start - 1] = '<') then
        begin
          case Text[Start + Length(AName)] of
            '/': Exit(ADefault);
            '>':
            begin
              Inc(Start, Length(AName) + 1);
              Stop := Pos('</' + AName + '>', Text, Start);
              if Stop = 0 then
                Result := ADefault
              else
                Result := Copy(Text, Start, Stop - Start);
              Exit;
            end;
          end;
        end;
        Start := Pos(AName, Text, Start + 1);
      end;
      Result := ADefault;
    end;
     
    function TFastXML.GetAttribute(const ANode, AName: string): string;
    var
      Start, Stop, Attr, Value: Integer;
    begin
      Start := Pos('<' + ANode, Text);
      while Start > 0 do
      begin
        Stop := Pos('>', Text, Start);
        Attr := Pos(AName + '="', Text, Start);
        if (Attr > 0) and (Attr < Stop) then
        begin
          Inc(Attr, Length(AName) + 2);
          Value := Pos('"', Text, Attr);
          if (Value > 0) and (Value < Stop) then
          begin
            Result := Copy(Text, Attr, Value - Attr);
            Exit;
          end;
        end;
        Start := Pos('<' + ANode, Text, Start + 1);
      end;
      Result := '';
    end;
     
    function TFastXML.GetBool(const AName: string; ADefault: Boolean): Boolean;
    var
      Value: string;
    begin
      Value := GetString(AName);
      if Value = '' then
        Result := ADefault
      else
        Result := Value = 'TRUE';
    end;
     
    function TFastXML.GetInt(const AName: string; ADefault: Integer): Integer;
    var
      Value: string;
    begin
      Value := GetString(AName);
      if Value = '' then
        Result := ADefault
      else
        Result := StrToInt(Value);
    end;
     
    end.
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

Discussions similaires

  1. Impossible de trouver l'Explorateur de schémas XML
    Par goute dans le forum EDI/Outils
    Réponses: 1
    Dernier message: 04/04/2014, 11h03
  2. ajoutter un noeud dans un xml existant
    Par etud_ini dans le forum VB.NET
    Réponses: 1
    Dernier message: 26/10/2008, 21h08
  3. [DOM] Ajouter un noeud dans un XML existant
    Par splifferwolf dans le forum Bibliothèques et frameworks
    Réponses: 11
    Dernier message: 11/02/2008, 17h01
  4. XML noeud existe
    Par horzy dans le forum VB.NET
    Réponses: 4
    Dernier message: 06/07/2007, 17h00
  5. Réponses: 10
    Dernier message: 12/05/2006, 11h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo