[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

Reading Recursive XML with PHP and inserting mysql

Hello

i have a xml document like below
<category_list>
<category id="1">
       <title>Category 1</title>
       <category id="2">
             <title>Category 1.1</title>
       </category>
       <category id="3">
              <title>Category 1.2</title>
              <category id="4">
                  <title>Category 1.2.1</title>
              </category>
       </category>
</category>
</category_list>

i want to parse this xml and write it to mysql. in mysql i have category table and have two columns

category_id,parent_id,category_name

after parsing and inserting values two table result will be like below

category_id,parent_id,category_name
1,                   0,                  Category 1
2,                   1,                  Category 1.1
3,                   1,                  Category 1.2
4,                   3,                  Category 1.2.1

Parsing method is not important but if it be Simple_xml or DOM it will be nice.

Thanks Please Help


0
phparmy
Asked:
phparmy
  • 3
2 Solutions
 
Roger BaklundCommented:
Try this:
$xml = <<<XML
<category_list>
<category id="1">
       <title>Category 1</title>
       <category id="2">
             <title>Category 1.1</title>
       </category>
       <category id="3">
              <title>Category 1.2</title>
              <category id="4">
                  <title>Category 1.2.1</title>
              </category>
       </category>
</category>
</category_list>
XML;
 
function get_cat($node,$parent=0) {
  echo "insert into category set 
    category_id={$node['id']}, 
    parent_id=$parent, 
    category_name='{$node->title}'" . '<br />';
  $parent = $node['id'];
  foreach($node->category as $cat) 
    get_cat($cat,$parent); # recursive
}
 
$p = new SimpleXMLElement($xml);
get_cat($p->category);

Open in new window

0
 
Roger BaklundCommented:
It will of course only echo the insert statements, if it looks good, replace "echo" with mysql_query() and remove the '<br />'.
0
 
phparmyAuthor Commented:
Thanks but its not work for below if there is Category 2, Category 3 its only works for Category 1

$xml = <<<XML
<category_list>
<category id="1">
       <title>Category 1</title>
       <category id="2">
             <title>Category 1.1</title>
       </category>
       <category id="3">
              <title>Category 1.2</title>
              <category id="4">
                  <title>Category 1.2.1</title>
              </category>
       </category>
       <category id="5">
                   <title>Category 1.3</title>
       </category>
</category>
<category id="6">
      <title>Category 2</title>
</category>
<category id="7">
      <title>Category 3</title>
</category>
</category_list>
XML;
0
 
Roger BaklundCommented:
Then you have multiple nodes with parent=0 . Call it from a loop:

$p = new SimpleXMLElement($xml);
foreach($p->category as $node)  
  get_cat($node);
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now