[SOLVED][PHP/FILES/XML] Deviding files

Junior Spellweaver
Joined
Feb 8, 2009
Messages
112
Reaction score
0
Hello everyone. I need help with php scripting. Here is thing.

I have file:
Code:
<items><variable name="id" type="int">9564</variable>
<variable name="pos" type="int">1</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9566</variable>
<variable name="pos" type="int">2</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9568</variable>
<variable name="pos" type="int">3</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9570</variable>
<variable name="pos" type="int">4</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9572</variable>
<variable name="pos" type="int">5</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9582</variable>
<variable name="pos" type="int">6</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9584</variable>
<variable name="pos" type="int">7</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9590</variable>
<variable name="pos" type="int">8</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9592</variable>
<variable name="pos" type="int">9</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9594</variable>
<variable name="pos" type="int">10</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9602</variable>
<variable name="pos" type="int">11</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9574</variable>
<variable name="pos" type="int">12</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9576</variable>
<variable name="pos" type="int">13</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9578</variable>
<variable name="pos" type="int">14</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9586</variable>
<variable name="pos" type="int">15</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9588</variable>
<variable name="pos" type="int">16</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9596</variable>
<variable name="pos" type="int">17</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9598</variable>
<variable name="pos" type="int">18</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9604</variable>
<variable name="pos" type="int">19</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9606</variable>
<variable name="pos" type="int">20</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9608</variable>
<variable name="pos" type="int">21</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9610</variable>
<variable name="pos" type="int">22</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">10060</variable>
<variable name="pos" type="int">23</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9600</variable>
<variable name="pos" type="int">24</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
<items><variable name="id" type="int">9580</variable>
<variable name="pos" type="int">25</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>

And i need to make a php script which will open that file and divide it in separate files. But i need to divide it specifically. It should start reading file and look for <items> and copy from beginning of <items> through end of </items>. And then save it to new file with its id.


Here is example
it will copy this:
Code:
<items><variable name="id" type="int">9580</variable>
<variable name="pos" type="int">25</variable>
<variable name="count" type="int">1</variable>
<variable name="max_count" type="int">1</variable>
<variable name="data" type="Octets"></variable>
<variable name="proctype" type="int">0</variable>
<variable name="expire_date" type="int">0</variable>
<variable name="guid1" type="int">1202570355</variable>
<variable name="guid2" type="int">1244</variable>
<variable name="mask" type="int">0</variable>
</items>
and then look for id after type="int"> until </variable> in this case it would be 9580.


i need this script because a got like 200files and 30 XML codes in each files, and i need add each XML code into mysql database. it would be cool if anyone could help me with this. i know how to open file and write it to mysql but i dono how to make it copy specific partion of it and save it to a file with its id or do something else with it.

p.s. sorry if it doesnt make seance. ask questions and i will answer if something isnt clear


======== Added +++
after some research i found this http://us.php.net/manual/en/function.file.php
and also figured out that using C++ or other programming language will be easier.
 
Last edited:
Re: [PHP/FILES/XML] Deviding files

Here is what i got so far:
PHP:
<?php

function connect_to_mysql($my_host, $my_user, $my_pass, $my_db) {
	$mysql_conn = mysql_connect($my_host, $my_user, $my_pass);
	if (!$mysql_conn) { echo "MySQL: Connect Error"; }
	
	$mysql_db = mysql_select_db($my_db, $mysql_conn);
	if (!$mysql_db) { echo "MySQL: Select DB Error"; }
	
return true;
}

connect_to_mysql('192.168.0.2', 'user', 'user', 'xml');
$file = @fopen("test.txt", "r");



$i='0';
$xmlcode = "";

while($i < 242) {
$xmlcode = fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
echo '<font color="red"><xmp>'.$xmlcode.'</xmp></font>';
echo '------------------------------------<br>';
mysql_query("INSERT INTO `items` VALUES ($i, '$xmlcode');") or die(mysql_error());  

$i = $i + 11;
}
?>

It reads 11 lines and adds it to mysql database. now i need to know how to make it use id from code.
 
Re: [PHP/FILES/XML] Deviding files

Got it working. Here is final result in case someone needs it:

PHP:
<?php

function connect_to_mysql($my_host, $my_user, $my_pass, $my_db) {
	$mysql_conn = mysql_connect($my_host, $my_user, $my_pass);
	if (!$mysql_conn) { echo "MySQL: Connect Error"; }
	
	$mysql_db = mysql_select_db($my_db, $mysql_conn);
	if (!$mysql_db) { echo "MySQL: Select DB Error"; }
	
return true;
}

connect_to_mysql('192.168.0.2', 'user', 'user', 'xml');
$file = @fopen("test.txt", "r");



$i='0';
$xmlcode = "";

while($i < 242) {
$xmlid = "";
$xmlcode = "";
$xmlcode = fgets($file);
$xmlid = substr($xmlcode, 38, -11);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
$xmlcode = $xmlcode.fgets($file);
echo '------------------------------------------------------------------------------<br>';
echo '---------------- '.$xmlid.'<br>';
echo '<font color="red"><xmp>'.$xmlcode.'</xmp></font>';
mysql_query("INSERT INTO `items` VALUES('$xmlid', '$xmlcode')");  

$i = $i + 11;
}
?>
 
Back