Jump to content

Translate XML Text


flashbangpro

Recommended Posts

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

Link to comment
Share on other sites

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! :)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.