• Index
  •  » Talend Open Studio for Data Integration » Usage, Operation
  •  » [resolved] Put Variables into an XML

#1 2009-06-09 14:52:42

hammerwd
Member
Registered: 2008-04-27
Posts: 59

[resolved] Put Variables into an XML

Tags: [filelist, folder, xml]

Hi,

I've got several folders. In each folder, there is a XML file.
Now I want to count the folders (let's say I've got 17 at this run).
After that I want to iterate through the folders, open the XML file search for the line:
<Attachment id="">
and put in the number of the current folder.
<Attachment id="01"> for the first folder, <Attachment id="02"> for the second, and so on.
The order doesn't matter.

How can I do this?
I guess I need fileList and there is a loop component. Can I use them together for this purpose?

Thank you in advance.
Bye, CHris

Offline

#2 2009-06-10 08:44:22

shong
Talend team
Registered: 2007-08-29
Posts: 10297
Website

Re: [resolved] Put Variables into an XML

Hello Chris
You need to use one tFileList to iterate folders and another tFileList to iterate file, then write some Java code to search the string and insert a specify value into it.
Here I show you an example, I search the following line and put the current folder name in it. eg:
Assuming there is a file: out1.xml in folder: D:\file\test2\folder1
I search the specify line and put the folder name in it,
<Attachment id="folder1">

First, go to Resipotory-->code-->Routines and create a routine call: forum6953

Code:

package routines;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class forum6953 {

    public static void serarchAndEdit(String folderPath, String filePath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        String folderName = folderPath
                .substring((folderPath.lastIndexOf("\\") + 1));
        System.out.println(folderName);

        try {
            fis = new FileInputStream(filePath);
            byte[] b = new byte[fis.available()];

            fis.read(b);
            String s = new String(b);
            s = s.replaceFirst("<Attachment id=\"\">", "<Attachment id=\""
                    + folderName + "\">");

            b = s.getBytes();

            fos = new FileOutputStream(filePath);
            fos.write(b);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Please see my screenshots

Best regards

         shong


Uploaded Images


Email:shong@talend.com
Choose Talend, Enjoy Talend!
New & Event: Talend Help Center
Talend-->the leader of open source data management and application integration solutions!

Offline

#3 2009-06-10 14:14:32

hammerwd
Member
Registered: 2008-04-27
Posts: 59

Re: [resolved] Put Variables into an XML

Hi,

thank you very much.
But unfortunately my folders have an ID .. like 1681631 or 98357 so I can't use these names.
That's why I came up with the tLoop component.

Can I access the current variable from tLoop (depending on whether there is a folder or not) inside your routine?

Like:

Code:

i = 1;
while ( folder.exists () )
{
  id = i;
  folder.next ();
  i++;
}

Bye, Chris

Offline

#4 2009-06-10 14:34:04

shong
Talend team
Registered: 2007-08-29
Posts: 10297
Website

Re: [resolved] Put Variables into an XML

Hello

But unfortunately my folders have an ID .. like 1681631 or 98357 so I can't use these names

What's your real folder name? Why do you say ' I can't use these names'? What are your expected result?

Can you describe your request more precise? Some screenshot will be better

Best regards

          shong


Email:shong@talend.com
Choose Talend, Enjoy Talend!
New & Event: Talend Help Center
Talend-->the leader of open source data management and application integration solutions!

Offline

#5 2009-06-15 09:27:25

hammerwd
Member
Registered: 2008-04-27
Posts: 59

Re: [resolved] Put Variables into an XML

Hi,

ok, here are the screenshots
topfolder:
There is a XML named: 56-13638866.xml
subfolder:
I've got 0...n subfolders. Within theses subfolders, there is a XML and a JPG. Now I need to tell the XML the number of this subfolder.

Code:

<Attachment id="attachement_">
  <File>56-13638867.jpg</File>

Depending on the number of the folder (folder 1, folder 2, folder 3 - disregarding its name: 56-13638867) I want to put this number into the code to change it into:

Code:

<Attachment id="attachement_001">
  <File>56-13638867.jpg</File>

and the next one into:

Code:

<Attachment id="attachement_003">
  <File>56-13638867.jpg</File>

etc.

Hope I expressed it well?

Best regards,
Chris


Uploaded Images

Offline

#6 2009-06-15 10:15:28

shong
Talend team
Registered: 2007-08-29
Posts: 10297
Website

Re: [resolved] Put Variables into an XML

Hello

Hope I expressed it well?

Yes, I understand well, I make a little change on my routine to generate a sequence number for each xml file.

Code:

package routines;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class forum6953 {

    public static void serarchAndEdit(String folderPath, String filePath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        int id = Numeric.sequence("s1", 1, 1);

        try {
            fis = new FileInputStream(filePath);
            byte[] b = new byte[fis.available()];

            fis.read(b);
            String s = new String(b);
            s = s.replaceFirst("<Attachment id=\"\">", "<Attachment id=\"" + id
                    + "\">");

            b = s.getBytes();

            fos = new FileOutputStream(filePath);
            fos.write(b);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

If you know Java a little, you can modify it a little to fit your request more close.

Best regards
     
         shong


Email:shong@talend.com
Choose Talend, Enjoy Talend!
New & Event: Talend Help Center
Talend-->the leader of open source data management and application integration solutions!

Offline

#7 2009-06-15 11:45:05

hammerwd
Member
Registered: 2008-04-27
Posts: 59

Re: [resolved] Put Variables into an XML

Wow!

Absolutely incredible - it works.

Hen hao, xiexie ni - Thank you very much.

Offline

  • Index
  •  » Talend Open Studio for Data Integration » Usage, Operation
  •  » [resolved] Put Variables into an XML

Board footer

Powered by FluxBB