Recursive function to read each Node Xml file in PHP

I want the code to read the xml file and return all its node values ​​and check the node level too.print every node, and if the node level is 2 or more than 2, print a textbox in front of the node name. but I am not getting any node value. Let's assume in advance.

my code:

<!DOCTYPE html>
<html>
<head> INSERT VALUES OF THE GIVEN PHRASE IDS : </head>
<!-- <head> <link rel="stylesheet" type="text/css" href="style.css" /></head> -->
<body>

<form name="form" action="xmlform.php" method="post">

<?php

echo ("<pre>");

$file="english.xml";

if(file_exists($file))
{
  $xml=simplexml_load_file("$file") or die("Error: Cannot create object");

foreach ($xml as $xmlRoot => $value) {
    $lvl=0;
    $xmlNode=$value->children();

    //if(is_null($value->children())) {echo "true";}exit();
   echo '<input type="text" name="xmlNode" value="'.$xmlNode.'" > <br>';

  function makeAFieldForNode($xmlNode, $lvl) {
    if ($lvl >= 2) {
         echo '<input type="text" value="" name="value"> <br>';
    } 
        $newLvl =   $lvl++;
        foreach ( $xmlNode->children() as $xn) {
        makeAFieldForNode($xN, $newLvl);  
     }
} 

makeAFieldForNode();

}

}

echo ("</pre>");


?>
</form>
</body>
</html>  

      

english.xml

<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
   <product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
      <catalog_item gender="Men's">
         <size description="Medium">
            <color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
             <color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
         </size>
         <size description="Large">
           <color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
            <color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
         </size>
      </catalog_item>
   </product>
</catalog>

      

I need a form in the output, if you see the above xml file, I want something like this

catalog
     product
      123  (123 is id and text field for value infront of it )
                catalog-item
                     size
                         color_swatch
                          color (color id id of color_swatch and text field for value)

      

so if I add some value to the submit button, I can create a new XML file like the one that already exists, but with the new ID values ​​of the corresponding elements. I am currently trying to generate this form with all elements of the xml file. I want dynamic code so that any XML file is. my code is to read each element and read it, and also give an id and a textbox for the value. Hope you get my idea.

+3


source to share


1 answer


Be careful with the position of the increment operator:

$newLvl =   $lvl++;

      

$newLvl

will get the value $lvl

before incrementing (see http://3v4l.org/aj7t3 )

In your case, you don't even need a new variable, just execute $lvl++

and use it in your function call:

$lvl++;
foreach ( $xmlNode->children() as $xn) {
    makeAFieldForNode($xN, $lvl);  
}

      

Edit: to get a more complete answer, I need to know what you mean:

node values



and

print each node

as some of your nodes have no attribute value

( product

or size

).

If you could provide an example of the output you would like to get, it will help you understand your needs.

<h / "> == Edit: add script ==

Try this: http://3v4l.org/UspVk

+1


source







All Articles