Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

Read xml file content into an Attribute: How to?

avatar
Contributor

Hi, 

Please i need your kind help.

SCENERIO: INSERT CONTENT OF AN XML FILE AS A FIELD INTO A DATABASE

- I am trying to read the content of an xml file into a flowfile attribute

- I want to use the attribute as variable in sql query statement to insert the content into a DB

ISSUES:

- I tried to use both Xpath & Xquery but no success.

 

DATA: The data i am trying to convert is as below

 

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE database>
<database xmlns='http://www.lotus.com/dxl' version='9.0' maintenanceversion='1.0'
 replicaid='8525886700702AFD' path='BStravel2.nsf' title='BSTravel_Expenses_and_Vacations'
 maintainunread='false' optimizetablebitmaps='true' increasemaxfields='true'
 compressdesign='true' compressdata='true' markmodifiedunread='false' logtransactions='false'
 uselz1='true'>
<databaseinfo dbid='85256700702AFD' odsversion='43' diskspace='261440'
 percentused='96.2421875' numberofdocuments='13'><datamodified><datetime
 dst='true'>20220813253,61-04</datetime></datamodified><designmodified
><datetime dst='true'>2022062936,80-04</datetime></designmodified></databaseinfo>
<launchsettings><noteslaunch whenopened='openframeset' frameset='Launch' showaboutdocument='never'/></launchsettings></database>

 

 

Thank you so much for all your help .

1 ACCEPTED SOLUTION

avatar
Super Guru

@rafy 

 

You shouldn't need to extract the content as an attribute. Instead, use ReplaceText to replace the contents of the flowfile with the SQL template, like this:

INSERT INTO my_table (xml_col) VALUES ('$1')

 

Where $1 is a reference to the default regex capture group that captures the entire content.

In reality, it requires a bit more labour, since you have to escape the single quotes in the XML first.

 

Cheers,

André

 

--
Was your question answered? Please take some time to click on "Accept as Solution" below this post.
If you find a reply useful, say thanks by clicking on the thumbs up button.

View solution in original post

3 REPLIES 3

avatar
Super Guru

@rafy 

 

You shouldn't need to extract the content as an attribute. Instead, use ReplaceText to replace the contents of the flowfile with the SQL template, like this:

INSERT INTO my_table (xml_col) VALUES ('$1')

 

Where $1 is a reference to the default regex capture group that captures the entire content.

In reality, it requires a bit more labour, since you have to escape the single quotes in the XML first.

 

Cheers,

André

 

--
Was your question answered? Please take some time to click on "Accept as Solution" below this post.
If you find a reply useful, say thanks by clicking on the thumbs up button.

avatar
Contributor

Thanks for the insight Mr @araujo .

I would like to tell you i have thousands of that XML files. Please is there any better way of doing it.

Thank you.

avatar
Super Guru

@rafy ,

 

Same approach. Just create the processor(s) and let the files flow through it 🙂

 

Cheers,

André

 

--
Was your question answered? Please take some time to click on "Accept as Solution" below this post.
If you find a reply useful, say thanks by clicking on the thumbs up button.