flashbangpro Posted November 4, 2011 Share Posted November 4, 2011 Ok I am a student and my instructor keeps giving us projects dealing with PHP coding even though only half our class has had a PHP server side scripting class. The project he just gave us I thought would be simple, but I'm still struggling to find online the proper scripting to get my php file to do what I need it to do. Requirements : Create a simple html page that contains a text area with a submit buton - DONE The submit button will call a php page and display the text typed in the text area - DONE Allow users to enter XML script into the text area and when submit button is clicked the php page will display the XML without the XML tags, Example - Text Area Entry Would Look Like - <fname>James</fname><lname>Mays</lname> Output Would Look Like - James Mays This is the part I'm struggling with, I don't know how to tell the php to not display the XML tags - NOT DONE If this helps here is what code I do have, HTML Page <!DOCTYPE HTML> <html> <head> </head> <body> <form action="test.php" method="post"> <p>Text Area:</p> <textarea name="xmltags" rows="10" cols="40"></textarea></p> <p><input type="submit" value="Submit"></p> </form> </div> </div> </body> </html> PHP Code <html> <body> Text Output: <?php echo $_POST["xmltags"]; ?>!<br /> </body> </html> Thank You for any and all help Quote Link to comment https://forums.phpfreaks.com/topic/250471-translate-xml-text/ Share on other sites More sharing options...
mpsn Posted November 5, 2011 Share Posted November 5, 2011 There are two methods I know, SimpleXML (built into php core libraries) and pure DOM (I prefer DOM b/c more advanced), anyways here is snippet to give you idea what to do: $dom=new DOMDocument(); $dom->loadXML(THIS IS THE TEXT from text area when user submits);//and I think you need to link with $_POST[] to retrieve that info (loadXML will load a string as a dom document to parse) now build a loop to traverse each node starting with $dom (the root element node): function recursionTraverse($dom) { //check if attributes if($dom->hasAttributes() ), then: for($i=0;$i<$dom->attributes->length;$i++), do: print $dom->attributes->item($i) //check if text nodes (remember, white space is considered text node so use trim() to not output blank text nodes and print the //text node and element/tag node at the same time for($i=0;$i<$dom->childNodes->length;$i++), do: print $dom->childNodes->item($i)->nodeName//elem/tag node print $dom->childNodes->item($i)->nodeValue//Text node if($dom->hasChildNodes) recursionTraverse($dom->childNodes->item($i)) }END FUNCTION So something like this, the important thing to understand is that b/c of nature of an xml document, recursion is essential to traverse these kind of files! Quote Link to comment https://forums.phpfreaks.com/topic/250471-translate-xml-text/#findComment-1285112 Share on other sites More sharing options...
flashbangpro Posted November 7, 2011 Author Share Posted November 7, 2011 Thank you that does help explain things a lot more. I have just found out another requirement on the output is row names for each value. For example: <fname>James</fname> <lname>White</lname> Will output: fname James lname White Quote Link to comment https://forums.phpfreaks.com/topic/250471-translate-xml-text/#findComment-1285916 Share on other sites More sharing options...
silkfire Posted November 7, 2011 Share Posted November 7, 2011 strip_tags()? Quote Link to comment https://forums.phpfreaks.com/topic/250471-translate-xml-text/#findComment-1286033 Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 I'm going to go against mx760 for the same reason. I like SimpleXML because it's easier. <?php $xmldata = ' <body> <person> <fname>John</fname> <lname>Doe</lname> </person> <person> <fname>Jane</fname> <lname>Smith</lname> </person> <person> <fname>Homer</fname> <lname>Simpson</lname> </person> </body> '; // Initiate SimpleXML object // http://php.net/manual/en/class.simplexmlelement.php $xml = new SimpleXMLElement( $xmldata ); // Output the structure of the XML file we just loaded, wrapped in <pre> tags to figure out what we need echo '<pre>'; print_r( $xml ); echo '</pre>'; // Use a foreach to loop through $xml->person and put each <person> into the $person variable foreach( $xml->person as $person ) { // Use another foreach loop to loop through each member of <person> foreach( $person as $tag => $value ) { echo $tag . ' ' . $value . '<br>'; } } ?> Is pretty much what you want. Please let me know if there's any part of this you don't understand. If you just copy and paste this, you'll pass the assignment but fail the test. Quote Link to comment https://forums.phpfreaks.com/topic/250471-translate-xml-text/#findComment-1286049 Share on other sites More sharing options...
flashbangpro Posted November 8, 2011 Author Share Posted November 8, 2011 Thank you, I am hoping to actually gain knowledge and not just get the answer right, from what I have researched there are amazing things that can be done with PHP. Knowing what the code does and seeing it like you have displayed helps me to learn how it functions. I will still need to edit it to get the information from the text area of the html page, cause they have to be separate pages, so please don't think I am just gonna steal your code and give no credit. Will this also work for any xml tag for example <age>30</age>, I just used <fname> and <lname> as examples. Again thank you for the help in understanding. Quote Link to comment https://forums.phpfreaks.com/topic/250471-translate-xml-text/#findComment-1286084 Share on other sites More sharing options...
xyph Posted November 8, 2011 Share Posted November 8, 2011 Try before asking Quote Link to comment https://forums.phpfreaks.com/topic/250471-translate-xml-text/#findComment-1286099 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.