Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert a txt file to XML

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
0
0
i have a text file contains ie:
# my_first_title #
my first subtext .
my second subtext .
my third subtext .

# my_second_title #
my A_first subtext .
my A_second subtext .
my A_therd subtext .
etc...

Question: How to convert it to XML using php? to :

<title> my_first_title </title>
<text>my first subtext .</text>
<text>my second subtext .</text>
<text>my third subtext .</text>

<title> my_second_title </title>
<text>my A_first subtext .</text>
<text>my A_second subtext .</text>
<text>my A_therd subtext .</text>
etc...

then I can manage the data and store it in a DB.

any help or code?
Thanks in advance.
 
why do you need to convert to xml before you insert into a database? is the real question not how you can parse the text file into identifiable chunks?

Code:
function translate2xms($file){

 //fix for mac for line endings
 $inival = ini_get('auto_detect_line_endings');
 ini_set('auto_detect_line_endings', true);

 //get contents in array
 $lines = file($file,  FILE_SKIP_EMPTY_LINES && FILE_IGNORE_NEW_LINES);
 //create output variable
 $output = '';
 foreach($lines as $line){
  $line = trim ($line); //get rid of whitespace
  if ( (substr($line, 0, 1) === #) && (substr($line, -1) === #)){
    $line = trim($line, '#'); //get rid of hashes
    $output .= "<title>$line</title>\r\n";
  } else {
    $output .= "\t<text>$line</text>\r\n";
  } //end if
 } //end foreach
 //reset ini file
 ini_set ('auto_detect_line_endings', $inival);
 return $output;
} //end function
 
exactly,
To convert the file into sql dump:

<LID>1</LID>
<AYAID>1</AYAID><AYA>text1</AYA>
<AYAID>2</AYAID><AYA>text2</AYA>
<AYAID>3</AYAID><AYA>text4</AYA>
<AYAID>4</AYAID><AYA>text5</AYA>

<LID>2</LID>
<AYAID>1</AYAID><AYA>text1</AYA>
<AYAID>2</AYAID><AYA>text2</AYA>
<AYAID>3</AYAID><AYA>text4</AYA>
<AYAID>4</AYAID><AYA>text5</AYA>

etc ...

i want to print : insert into my_table (lid,ayaid,aya) values ('$LID','$AYAID','$AYAID');
output:
insert into my_table (lid,ayaid,aya) values ('1','1','text1');
insert into my_table (lid,ayaid,aya) values ('1','2,'text2');
etc ..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top