ComposantsComponents

ComposantsComponents › Fichier › SortieFile › Output

tFileOutputMSXML

tFileOutputMSXML

Fichier › SortieFile › Output

Écrit les lignes de plusieurs flux dans un seul document XML. Chaque flux a son propre arbre, dessiné dans l'éditeur du composant à partir d'un élément racine commun : ses lignes vont dans une branche à part, ou, avec un élément de groupe, dans les éléments créés par un flux précédent (les lignes d'une commande dans sa commande). Le document est construit en mémoire, puis écrit à la fin. Writes the rows of several flows into one XML document. Each flow has its own tree, drawn in the editor of the component from a common root element: its rows go into a separate branch, or, with a group element, into the elements created by a previous flow (the lines of an order inside their order). The document is built in memory, then written at the end.

EntréeInput

Merge un lien par flux, au moins un, sans limite ; l'ordre des liens compteone link per flow, at least one, no limit; the order of the links matters

SortieOutput

Aucune : pas de flux de sortie. Le résultat est le fichier écrit. None: no output flow. The result is the written file.

Déclencheurs en sortieOutput triggers

On Component Ok On Component Error Run if

UtilisationUsage

Commandes et lignes de commandeOrders and order lines

orders (Merge order:1) lines (Merge order:2) tFixedFlowInput_1 tFileOutputMSXML_1 tFixedFlowInput_2

Le flux parent (orders) est relié en premier : ses éléments doivent exister quand arrivent les lignes du flux enfant. The parent flow (orders) is connected first: its elements must exist when the rows of the child flow arrive.

  1. Déposez le tFileOutputMSXML depuis la famille Fichier › Sortie de la palette. Drop the tFileOutputMSXML from the File › Output family of the palette.
  2. Reliez-lui chaque flux (clic droit › Ligne › Main), le flux parent d'abord : les liens deviennent des liens Merge, numérotés dans leur libellé. Pour changer l'ordre ensuite : clic droit sur le composant › Modifier l'ordre de fusion. Connect each flow to it (right-click › Row › Main), the parent flow first: the links become Merge links, numbered in their label. To change the order afterwards: right-click the component › Modify the merge order.
  3. Ouvrez l'éditeur par le bouton de Configurer l'arbre XML. Une liste déroulante y choisit le flux : chacun a ses colonnes et son arbre. Tous les arbres commencent par le même élément racine, sinon OK refuse : « Les éléments racines de l'arbre XML doivent être les mêmes. » Open the editor with the button of Configure XML Tree. A drop-down list there chooses the flow: each one has its columns and its tree. All the trees start with the same root element, otherwise OK refuses: “Root elements of XML tree should be the same.”
  4. Pour chaque flux, ajoutez éléments et attributs (clic droit › Ajouter un sous-élément, Ajouter un attribut), glissez les colonnes dessus, puis clic droit sur l'élément répété à chaque ligne › Définir comme élément de boucle. For each flow, add elements and attributes (right-click › Add Sub-element, Add Attribute), drag the columns onto them, then right-click the element repeated for each row › Set As Loop Element.
  5. Pour placer les lignes d'un flux enfant dans les éléments du parent : dans son arbre, reprenez l'élément répété du parent, avec le même chemin et la clé au même endroit (l'attribut id, lié ici à order_id), clic droit › Définir comme élément de groupe, et mettez l'élément de boucle dessous. To put the rows of a child flow into the elements of the parent: in its tree, repeat the repeated element of the parent, with the same path and the key at the same place (the id attribute, linked here to order_id), right-click › Set As Group Element, and put the loop element under it.

Où vont les lignesWhere the rows go

  • Les lignes sont ajoutées au document à leur arrivée : toutes celles du premier lien (Merge order:1), dans leur ordre, puis celles du deuxième, et ainsi de suite. Le fichier n'est écrit qu'à la fin. The rows are added to the document as they arrive: all those of the first link (Merge order:1), in their order, then those of the second one, and so on. The file is only written at the end.
  • Le premier lien crée l'élément racine. Un lien suivant ajoute sa branche sous le dernier élément que son arbre partage avec les arbres des liens précédents (voir l'exemple). The first link creates the root element. A following link adds its branch under the last element that its tree shares with the trees of the previous links (see the example).
  • Avec un élément de groupe, chaque ligne va dans le premier élément existant de même chemin dont les valeurs du groupe sont égales aux siennes. S'il n'y en a pas, un nouvel élément de groupe est créé : une ligne enfant sans parent n'est pas perdue, contrairement à tFileOutputMSDelimited (voir l'exemple). With a group element, each row goes into the first existing element with the same path whose group values equal its own. If there is none, a new group element is created: a child row without a parent is not lost, unlike tFileOutputMSDelimited (see the example).

Paramètres simplesBasic settings

Les libellés sont ceux de l'onglet Composant ; le nom technique, en gris, est celui du paramètre dans le fichier du job. Le composant n'a pas de schéma propre : il prend celui de chaque lien entrant. Labels are those of the Component tab; the technical name, in grey, is the parameter name in the job file. The component has no schema of its own: it takes the one of each incoming link.

ParamètreSetting DéfautDefault VisibleShown Description
Nom de fichierFile NameFILENAME chemin par défaut + /out.xmldefault path + /out.xml ToujoursAlways Le fichier écrit, en expression Java : context.folder + "/orders.xml". Il est remplacé s'il existe. The written file, as a Java expression: context.folder + "/orders.xml". It is replaced if it exists.
Configurer l'arbre XMLConfigure XML TreeMAP — ToujoursAlways Ouvre l'éditeur des arbres, un par flux. Les arbres sont enregistrés dans trois tables cachées du composant, ROOT, GROUP et LOOP (éléments au-dessus du groupe, groupe, boucle). Opens the editor of the trees, one per flow. The trees are saved in three hidden tables of the component, ROOT, GROUP and LOOP (elements above the group, group, loop).

Paramètres avancésAdvanced settings

ParamètreSetting DéfautDefault VisibleShown Description
Créer le répertoire seulement s'il n'existe pasCreate directory only if it does not existCREATE OuiYes ToujoursAlways Crée le dossier du fichier, avec ses parents, au démarrage du composant. Creates the folder of the file, with its parents, when the component starts.
Créer un élément vide si nécessaireCreate empty element if neededCREATE_EMPTY_ELEMENT OuiYes ToujoursAlways Coché, un élément dont la colonne vaut null est écrit vide (<name/>). Décoché, les éléments vides sont retirés du document (voir les exemples). Checked, an element whose column is null is written empty (<name/>). Unchecked, the empty elements are removed from the document (see the examples).
Séparateur avancé (pour les nombres)Advanced separator (for numbers)ADVANCED_SEPARATOR NonNo ToujoursAlways Écrit les nombres avec les séparateurs ci-dessous (d'après le modèle, non essayé). Writes the numbers with the separators below (according to the template, not tried).
Séparateur de milliersThousands separatorTHOUSANDS_SEPARATOR "," Séparateur avancé cochéAdvanced separator checked Un caractère, entre guillemets. One character, in quotes.
Séparateur décimalDecimal separatorDECIMAL_SEPARATOR "." Séparateur avancé cochéAdvanced separator checked Un caractère, entre guillemets. One character, in quotes.
EncodageEncodingENCODING "ISO-8859-15" ToujoursAlways L'encodage du fichier, repris dans la déclaration XML : <?xml version="1.0" encoding="ISO-8859-15"?>. The encoding of the file, repeated in the XML declaration: <?xml version="1.0" encoding="ISO-8859-15"?>.
Ne pas générer de fichier videDon't generate empty fileDELETE_EMPTYFILE NonNo ToujoursAlways Sans aucune ligne reçue, le fichier est supprimé après l'écriture (d'après le modèle, non essayé). With no row received, the file is deleted after the writing (according to the template, not tried).
Supprimer les caractères d'espaceTrim the whitespace charactersTRIM_TEXT OuiYes ToujoursAlways Retire les espaces en trop du texte des éléments : " Ada " devient Ada (voir l'exemple). Removes the excess spaces from the text of the elements: " Ada " becomes Ada (see the example).
Échapper le texteEscape TextESCAPE_TEXT OuiYes ToujoursAlways Écrit &, < et > du texte en &amp;, &lt;, &gt;. Décoché, ils sont écrits tels quels et le fichier n'est plus un XML valide (voir l'exemple). Writes &, < and > of the text as &amp;, &lt;, &gt;. Unchecked, they are written as is and the file is no longer valid XML (see the example).

Variable globaleGlobal variable

NB_LINE (Integer, disponible après l'exécution du composant) : nombre de lignes reçues, tous flux confondus. NB_LINE (Integer, available after the component has run): number of rows received, all flows together.

((Integer) globalMap.get("tFileOutputMSXML_1_NB_LINE"))

ExemplesExamples

Les flux viennent de tFixedFlowInput en mode tableau. Le fichier est écrit dans le dossier de la variable de contexte folder ; un tJava relié par On Subjob Ok au premier tFixedFlowInput le lit en ISO-8859-15 et l'affiche ligne par ligne, puis affiche NB_LINE. Dans les arbres, ← donne la colonne liée. Les sorties sont celles de la console, copiées depuis une exécution du job. The flows come from tFixedFlowInput components in table mode. The file is written in the folder of the folder context variable; a tJava connected with On Subjob Ok to the first tFixedFlowInput reads it in ISO-8859-15 and prints it line by line, then prints NB_LINE. In the trees, ← gives the linked column. The outputs are the console ones, copied from a run of the job.

Les lignes de commande dans leur commandeThe order lines inside their order

orders (id, customer) : 1 Ada, 2 Grace, 3 Alan. lines (order_id, product, qty) : 2 Ink 1, 1 Pen 3, 9 Glue 2, 1 Paper 10. Arbre de orders : orders (id, customer): 1 Ada, 2 Grace, 3 Alan. lines (order_id, product, qty): 2 Ink 1, 1 Pen 3, 9 Glue 2, 1 Paper 10. Tree of orders:

orders
└─ order      (boucleloop)   @id ← id
   └─ customer            ← customer

Arbre de lines :Tree of lines:

orders
└─ order      (groupegroup)  @id ← order_id
   └─ line    (boucleloop)   ← product, @qty ← qty
<?xml version="1.0" encoding="ISO-8859-15"?>

<orders>
  <order id="1">
    <customer>Ada</customer>
    <line qty="3">Pen</line>
    <line qty="10">Paper</line>
  </order>
  <order id="2">
    <customer>Grace</customer>
    <line qty="1">Ink</line>
  </order>
  <order id="3">
    <customer>Alan</customer>
  </order>
  <order id="9">
    <line qty="2">Glue</line>
  </order>
</orders>
NB_LINE: 7

Chaque ligne entre dans la commande dont l'attribut id vaut son order_id, après le client. La ligne Glue ne trouve pas de commande 9 : un élément <order id="9"> est créé pour elle, sans client. Each line goes into the order whose id attribute equals its order_id, after the customer. The Glue line finds no order 9: an <order id="9"> element is created for it, without a customer.

Deux flux, deux branchesTwo flows, two branches

customers (id, name) : 1 Ada, 2 null. products (code, label) : P1 Pen, P2 Ink. Les deux arbres partagent la racine data : customers (id, name): 1 Ada, 2 null. products (code, label): P1 Pen, P2 Ink. Both trees share the data root:

data
└─ customers
   └─ customer   (boucleloop)  @id ← id
      └─ name                ← name

data
└─ products
   └─ product    (boucleloop)  @code ← code
      └─ label               ← label
<?xml version="1.0" encoding="ISO-8859-15"?>

<data>
  <customers>
    <customer id="1">
      <name>Ada</name>
    </customer>
    <customer id="2">
      <name/>
    </customer>
  </customers>
  <products>
    <product code="P1">
      <label>Pen</label>
    </product>
    <product code="P2">
      <label>Ink</label>
    </product>
  </products>
</data>
NB_LINE: 4

products partage data avec customers : sa branche est ajoutée sous data, après celle des clients. products shares data with customers: its branch is added under data, after the customers one.

Sans élément videWithout empty element

Le même job, Créer un élément vide si nécessaire décoché : The same job, Create empty element if needed unchecked:

<?xml version="1.0" encoding="ISO-8859-15"?>

<data>
  <customers>
    <customer id="1">
      <name>Ada</name>
    </customer>
    <customer id="2"/>
  </customers>
  <products>
    <product code="P1">
      <label>Pen</label>
    </product>
    <product code="P2">
      <label>Ink</label>
    </product>
  </products>
</data>
NB_LINE: 4

L'élément <name/> du client 2 a disparu ; le client reste, il a un attribut. The <name/> element of customer 2 is gone; the customer stays, it has an attribute.

Échappement et espacesEscaping and spaces

Deux composants, chacun avec un flux d'une colonne label : Pen & <Ink> et " Ada " ; arbre items › item (boucle) ← label. Le premier garde les réglages par défaut, le second décoche Échapper le texte et Supprimer les caractères d'espace. Le tJava affiche chaque ligne entre crochets : Two components, each one with a flow of one label column: Pen & <Ink> and " Ada "; tree items › item (loop) ← label. The first one keeps the default settings, the second one unchecks Escape Text and Trim the whitespace characters. The tJava prints each line between brackets:

text_default.xml
[<?xml version="1.0" encoding="ISO-8859-15"?>]
[]
[<items>]
[  <item>Pen &amp; &lt;Ink&gt;</item>]
[  <item>Ada</item>]
[</items>]
text_raw.xml
[<?xml version="1.0" encoding="ISO-8859-15"?>]
[]
[<items>]
[  <item>Pen & <Ink></item>]
[  <item>  Ada  </item>]
[</items>]

ErreursErrors

Le nom du job est remplacé par MyJob, les chemins sont abrégés. The job name is replaced with MyJob, paths are shortened.

Bibliothèque xmlUtil introuvablexmlUtil library not found

Le composant a besoin de mvn:org.talend.libraries/xmlUtil/6.0.0, qui n'est sur aucun dépôt Maven public. Le Studio la livre dans son plugin org.talend.libraries.xml (fichier lib/xmlUtil.jar). Exécuté hors du Studio sans elle, le job du deuxième exemple ne compile pas : The component needs mvn:org.talend.libraries/xmlUtil/6.0.0, which is on no public Maven repository. The Studio ships it in its org.talend.libraries.xml plugin (file lib/xmlUtil.jar). Run outside the Studio without it, the job of the second example does not compile:

Library not found: mvn:org.talend.libraries/xmlUtil/6.0.0
MyJob.java:1251: error: package org.talend.util.xml does not exist
				isXML10Char_tFileOutputMSXML_1 = org.talend.util.xml.XMLText.isValid(
				                                                    ^
…
4 errors

Copiez xmlUtil.jar du Studio dans le dépôt Maven local, sous org/talend/libraries/xmlUtil/6.0.0/xmlUtil-6.0.0.jar. Les deux autres bibliothèques, dom4j 2.1.3 et jaxen 1.2.0, viennent de Maven Central. Copy xmlUtil.jar from the Studio into the local Maven repository, under org/talend/libraries/xmlUtil/6.0.0/xmlUtil-6.0.0.jar. The two other libraries, dom4j 2.1.3 and jaxen 1.2.0, come from Maven Central.

Flux enfant relié avant le parentChild flow connected before the parent

Le premier exemple, mais lines relié en premier (Merge order:1) et orders en second, les arbres inchangés : The first example, but lines connected first (Merge order:1) and orders second, the trees unchanged:

<?xml version="1.0" encoding="ISO-8859-15"?>

<orders>
  <order id="2">
    <line qty="1">Ink</line>
  </order>
  <order id="1">
    <line qty="3">Pen</line>
    <line qty="10">Paper</line>
  </order>
  <order id="9">
    <line qty="2">Glue</line>
  </order>
  <order id="1">
    <customer>Ada</customer>
  </order>
  <order id="2">
    <customer>Grace</customer>
  </order>
  <order id="3">
    <customer>Alan</customer>
  </order>
</orders>
NB_LINE: 7

Les lignes arrivent les premières et créent leurs propres éléments order ; les commandes, qui n'ont pas de groupe, en ajoutent d'autres. Aucune erreur ne le signale : reliez toujours le flux parent en premier. The lines arrive first and create their own order elements; the orders, which have no group, add other ones. No error reports it: always connect the parent flow first.

Conseils et limitesTips and limits

Tout le document en mémoireThe whole document in memory

Le document entier est construit en mémoire (dom4j) et écrit à la fin. Pour un gros volume, prévoyez la mémoire de la JVM (-Xmx). The whole document is built in memory (dom4j) and written at the end. For a large volume, plan the JVM memory (-Xmx).

Parents créés pour les orphelinsParents created for orphans

Une ligne enfant sans parent crée un élément de groupe à elle, comme <order id="9">. Pour écarter ces lignes, faites la jointure avant le composant (tJoin, avec ses rejets). A child row without a parent creates a group element of its own, like <order id="9">. To discard these rows, do the join before the component (tJoin, with its rejects).

Un seul fluxA single flow

Le composant marche aussi avec un seul lien. Pour un seul flux, tFileOutputXML suffit. The component also works with a single link. For a single flow, tFileOutputXML is enough.

Gardez l'échappementKeep the escaping

Laissez Échapper le texte coché, sauf si les colonnes contiennent déjà du XML échappé : un & seul rend le fichier illisible pour un lecteur XML. Leave Escape Text checked, unless the columns already hold escaped XML: a lone & makes the file unreadable for an XML reader.

Voir aussiSee also