3

I want a code to read xml file and get all its node values recursively and check the level of nodes too.print every node and if level of node is 2 or more than 2 print a text field in front of node name. but i am not getting any node value.thank you 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 want a form in output if u see above xml file i want some thing 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 on submission button i can generate a new xml file like the one which already exist but with new values of ids of respective elements. For right now i am trying to generate that form with all elements of xml file. I want a dynamic code so whatever xml file it is . my code should read every element and read it and give id and text field for value.I hope you are getting my point.

1 Answer 1

1

Be carefull with the position of increment operator :

$newLvl =   $lvl++;

$newLvl will get the value of $lvl before increment (see http://3v4l.org/aj7t3)

In your case, you don't even need a new variable, just do $lvl++ and use it in your function call :

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

Edit : to make a more complete answer, I'd need to know what you mean by :

node values

and

print every node

as some of your nodes doesn't have value attribute (product or size for example).

If you could provide an example of the output you want to get, that would help to understand your need.


== Edit : add script ==

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

Sign up to request clarification or add additional context in comments.

2 Comments

i want a form in output if u see above xml file i want some thing 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)
@RozeenaIbrahim Added a link to a possible solution that you can test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.