Question Julia Pertin · Oct 5, 2023

Parcours de noeuds XML

Bonjour, 

Je récupère un fichier XML structuré de cette façon : 

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <tablename>
        <Table>Erp.BRIEFS</Table>
        <Action>HAjoute</Action>
        <CleIris>IDBRIEFS</CleIris>
        <nIdentifiant>2</nIdentifiant>
        <IdIris>137</IdIris>
        <Contenu></Contenu>
    </tablename>
    <tablename>
        <Table>Erp.COMMANDES</Table>
        <Action>HAjoute</Action>
        <CleIris>IDCOMMANDES</CleIris>
        <nIdentifiant>5</nIdentifiant>
        <IdIris>138</IdIris>
        <Contenu></Contenu>
    </tablename>
</data>

J'ai besoin de parcourir ces noeuds afin de récupérer la valeur du noeud <Table> afin de pouvoir faire un Correlate sur la classe correspondante pour y attribuer mes valeurs. 

Je n'arrive à récupérer que le premier noeud <data>. 

Pouvez-vous m'aider s'il vous plait. 

Comments

Lorenzo Scalese · Oct 5, 2023

Bonjour @Julia Pertin ,

Voici un exemple avec votre xml: 
La classe de test avec votre XML (par facilité pour je l'ai mis dans un XData): 

Class demo.correlate.Test
{

/// Do ##class(demo.correlate.Test).Test() As %StatusClassMethod Test()
{
	#dim data As demo.correlate.structure.Data
	#dim tableName As demo.correlate.structure.TableName
	
	Set xmlTest = ##class(%Dictionary.CompiledXData).%OpenId($classname()_"||XMLTest",,.sc).Data
	Quit:$$$ISERR(sc) sc
	
	Set reader=##class(%XML.Reader).%New()
	Do reader.Correlate("data","demo.correlate.structure.Data")
	Set sc = reader.OpenStream(.xmlTest)
	Do reader.Next(.data)
		
	Set key = ""Set tableName = data.tablename.GetNext(.key)
	While key '= "" {
		Write !,"+ table ", tableName.Table
		Set tableName = data.tablename.GetNext(.key)
	}
	
	Quit$$$OK
}

XData XMLTest
{
<?xml version="1.0" encoding="UTF-8"?>
<data>
    <tablename>
        <Table>Erp.BRIEFS</Table>
        <Action>HAjoute</Action>
        <CleIris>IDBRIEFS</CleIris>
        <nIdentifiant>2</nIdentifiant>
        <IdIris>137</IdIris>
        <Contenu></Contenu>
    </tablename>
    <tablename>
        <Table>Erp.COMMANDES</Table>
        <Action>HAjoute</Action>
        <CleIris>IDCOMMANDES</CleIris>
        <nIdentifiant>5</nIdentifiant>
        <IdIris>138</IdIris>
        <Contenu></Contenu>
    </tablename>
</data>
}

}

Ensuite les deux classes correspondantes à la structure du xml (Data et TableName):

Class demo.correlate.structure.Data Extends (%RegisteredObject, %XML.Adaptor)
{

Parameter XMLNAME = "data";Property tablename As list Of demo.correlate.structure.TableName(XMLPROJECTION = "ELEMENT");
}
Class demo.correlate.structure.TableName Extends (%RegisteredObject, %XML.Adaptor)
{

Property Table As%String;Property Action As%String;Property CleIris As%String;Property nIdentifiant As%String;Property IdIris As%String;Property Contenu As%String;
}

En exécutant la méthode Test dans le terminal, elle affichera le contenu de property "Table":

USER>Do##class(demo.correlate.Test).Test()
 
+ table Erp.BRIEFS
+ table Erp.COMMANDES

J'espère que cet exemple vous aidera dans votre developpement.

Bonne journée.

0
Julia Pertin  Oct 5, 2023 to Lorenzo Scalese

Merci @Lorenzo Scalese ,

En revanche je me retrouve avec un "Method does not exist" lié au reader.Next 

J'ai pourtant mes 2 classes : 

Class replic.msg.XML Extends (%Persistent, %XML.Adaptor, Ens.Request)
{
Property Table As%String(MAXLEN = "");Property Action As%String(MAXLEN = "");Property CleIris As%String;Property nIdentifiant As%String;Property IdIris As%String;Property Contenu As%String;
}
Class replic.msg.XMLStructure Extends%Persistent
{
Parameter XMLNAME = "data";Property tablename As list Of replic.msg.XML(XMLPROJECTION = "ELEMENT");
}
Method readXML(pRequest As Ens.StreamContainer, pResponse As replic.msg.XMLStructure) As%Status
{
    set tsc             = $$$OKset reader          = ##class(%XML.Reader).%New()

    //set pResponse       = ##class(replic.msg.XMLStructure).%New()#dim data  As replic.msg.XMLStructure
	#dim tableName As replic.msg.XML
    do pRequest.Stream.Rewind()

    do reader.Correlate("data","replic.msg.XMLStructure")
    set status              = reader.OpenStream(pRequest.Stream)
    
    Do reader.Next(.data)

    Set key = ""Set tableName = data.tablename.GetNext(.key)
	While key '= "" {
		set^debug() = tableName.Table
		Set tableName = data.tablename.GetNext(.key)
	}
	}

Mon erreur : 

ERREUR <Ens>ErrException: <METHOD DOES NOT EXIST>Next+25^%XML.Reader.1 *XMLNew,replic.msg.XMLStructure -- - connecté en tant que '-' numéro - @'' 

0
Julia Pertin  Oct 5, 2023 to Julia Pertin

Cela fonctionne parfaitement en fait il me manquait le XML Adaptor. 

Merci beaucoup

0
Lorenzo Scalese  Oct 5, 2023 to Julia Pertin

Ha no messages ce sont croisés.
Parfait! 
Content que ce soit réglé

0
Lorenzo Scalese  Oct 5, 2023 to Julia Pertin

Re @Julia Pertin ,

Il manque %XML.Adaptor sur replic.msg.XMLStructure, essayez comme ceci : 

Class replic.msg.XMLStructure Extends (%Persistent, %XML.Adaptor)
{
Parameter XMLNAME = "data";Property tablename As list Of replic.msg.XML(XMLPROJECTION = "ELEMENT");
}
0