Reading xml post data/parsing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rkatl
Forum Newbie
Posts: 17
Joined: Tue Apr 22, 2008 8:20 pm

Reading xml post data/parsing

Post by rkatl »

Hi,

Not sure what is going on in my script. can someone help me here

I have two files, index.php and load.php. index.php has a form with text area which has an XML data.
When I submit the form, I call load.php. load.php reads the xml and supposed to parse the xml. I see an issue with parsing.

index.php code

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <form action="load.php" method="post">
        <textarea cols="80" rows="50" name="xmlString">
          <HTTP-POST>
             <LEAD> 
                    <NAME FIRST="Fred" LAST="Bloggs" NICK="Bloggsie" TITLE="Mr" />
                    <ADDRESS1> 123 street </ADDRESS1>
                    <ADDRESS2> 123 street </ADDRESS2>
                    <CITY> city </CITY>
                    <PHONE HOME="as text" WORK="text" CELL="text" />
                    <COUNTRY>usa </COUNTRY>
                    <POSTALCODE>12345</POSTALCODE>
                    <COMPANY>Full name of company here</COMPANY>
                    <GENDER>male</GENDER>
                    <BDAY>Add tags for year, month and day to make this more useful</BDAY>
                    <IP>1.11.111.11</IP>
                    <DOMAIN>www.abc.com</DOMAIN>
                    <EMAIL>Either use fixed tags like this, or change to a repeating tag</EMAIL>
                    <DATE>6/12/2009</DATE>
                    <VENDOR_ID>vendor1</VENDOR_ID>
              </LEAD>         
            </HTTP-POST>
        </textarea>
        <input type="submit" value="submit"/>
    </form>
</body>
</html>
 
load.php code

Code: Select all

<html>
<body>
<?php
if ( $_SERVER['REQUEST_METHOD'] === 'POST'){
 
    $data = $_POST["xmlString"];
    echo "Data: $data";
 
    $objDOM = new DomDocument($data);
 
  $note = $objDOM->getElementsByTagName("LEAD"); 
  // for each note tag, parse the document and get values for 
  // tasks and details tag. 
 
  foreach( $note as $value ) 
  { 
    $tasks = $value->getElementsByTagName("ADDRESS1"); 
    $task  = $tasks->item(0)->nodeValue; 
 
 
    $details = $value->getElementsByTagName("COUNTRY"); 
    $detail  = $details->item(0)->nodeValue; 
 
    echo "$task :: $detail <br>"; 
  }     
}
?>      
</body>
</html>
 
===========================================

Can someone advise me on what is wrong in load.php? For some reason I don't see xml data, I see plain text data that is why load.php is not parsing the content.
rkatl
Forum Newbie
Posts: 17
Joined: Tue Apr 22, 2008 8:20 pm

Re: Reading xml post data/parsing

Post by rkatl »

Excellent. It works.

Thank you so much, McInfo!
Post Reply