Jump to content

Parsing a text file in a loop into a mysql database

AmyLane

Hi everyone,

 

I've been learning PHP over the past month and I've hit a road block, I have a text file of data which i'd like to parse into a mysql database because there's a lot of data, I'm not sure if this is possible, the text file looks like this

 

<place name="Place 1">
        <category>Category</category>
        <description>Description</description>
        <link url="link" location="location"/>
</place>
<place name="Place 2">
        <category>Category</category>
        <description>Description</description>
        <link url="link" location="location"/>
</place>

 

if anyone could help me that would be most appreciated, thank you very much :)

Link to comment
Share on other sites

Link to post
Share on other sites

Try to read it as XML file, using php's xml functions. I'm not sure you pasted whole file, but if there is no xml tags you can always add them before using data for xml function.

Another option would be regular expressions, but that would need you to know how to prepare them.

 

I've done some quick test with simplexml_load_string and it works without <xml> tag on top but your data need to have root tag. But as I am writing this post I noticed that your data tags are inconstant, you have <title> opening tag but </game> closing tag, so, you may explain it more or think about regular expressions, or just strpos substr parsing. 

 

 

Edit: Ok I see you fixed your tags, so now <place> mach  </place> tag, you need to surround them with some root tag and simplexml_load_string will handle it. It will look like this:

<?xml version="1.0"?>
<root>
	<place name="Place 1">
		<category>Category</category>
		<description>Description</description>
		<link url="link" location="location"/>
	</place>
	<place name="Place 2">
		<category>Category</category>
		<description>Description</description>
		<link url="link" location="location"/>
	</place>
</root>

You can also add xml tag just to make it all correct. <root> isn't anything specific it can be whatever name you pick.

 

Edit2: Here is regex approach https://regex101.com/r/uE2sO8/1 It may be not te best query possible, but it gathers all the data, it should work as long as your attributes wont be empty, if you know that they may have be empty you may want cahnge .+ to .* as + matches one to infinite and * zero to infinite.

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Mr_KoKa said:

Edit: Ok I see you fixed your tags, so now <place> mach  </place> tag, you need to surround them with some root tag and simplexml_load_string will handle it. It will look like this:

You can also add xml tag just to make it all correct. <root> isn't anything specific it can be whatever name you pick.

Hi, sorry about that, I was typing it from memory as I don't have access to the text file until tomorrow, thank you very much, I will give it a try and hope for the best :)

Link to comment
Share on other sites

Link to post
Share on other sites

$xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");
$category = $ml->category;
$desc = $xml->description;

// sql here

I believe this should work

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×