Jump to content

xml to array


davemoody

Recommended Posts

I am trying really REALLY hard to load an element from an xml file into an array. It is for the purpose of testing username and password for a login system. I need to locate the correct username, and then compare the password. My idea is to find the username and then load all that entire element with all childs into an array (all elements needed for prefilling shopping cart form later) which can then be saved to the session. Obviously the username and password need to be tested together as well for login to succeed. The code below shows an experiment just to load a tag and print it out, can't even get that to work...

 

Here's the full code of my login file:

 

<?php

$error = false;

if(isset($_POST['login'])) {

$username = preg_replace('/[^A-Za-z0-9]/','', $_POST['username']);

$password = md5($_POST['password']);

 

$doc = new DomDocument();

$doc->load( "users/users.xml" );

 

$usernames = $doc->getElementsByTagName( "username" );

foreach( $Usernames as $username )

{

$usernames = $username->getElementsByTagName( "username" );

$username = $usernames->item(0)->nodeValue;

 

echo "<b>" . $username . "<p />";

}

}

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>[Login Page] Transform Your Life - Transform Your World...</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link rel="shortcut icon" href="images/favicon.ico" />

<link rel="stylesheet" type="text/css" href="style/template.css" />

</head>

<body>

<div>

<!--Header-->

<div class="header">

<?php

include 'header.php';

?>

<div class="infoblock2">

<form method="post" action="">

<ul class="login">

<li class="login2">Login:</li>

<li class="login2">Username <input type="text" name="username" size="25" /></li>

<li class="login2">Password <input type="password" name="password" size="25" /></li>

<li class="login2"><?php

if($error) {

echo '<p />Invalid Username and/or Password';

}

?>

<input type="submit" value="Login" name="login" /></li>

<li class="login2"><a href="registration.php">Register</a></li>

</ul>

</form>

</div>

</div>

</div>

</body>

</html>

 

 

 

And here's the xml file:

 

 

<?xml version="1.0"?>

<Users>

<User>

<password>098f6bcd4621d373cade4e832627b4f6</password>

<email>[email protected]</email>

<username>a</username>

<firstname>a</firstname>

<lastname>a</lastname>

<age>67</age>

<street>a</street>

<city>a</city>

<state>a</state>

<country>Thailand</country>

</User>

<User>

<password>7694f4a66316e53c8cdd9d9954bd611d</password>

<email>[email protected]</email>

<username>qq</username>

<firstname>q</firstname>

<lastname>q</lastname>

<age>33</age>

<street>q</street>

<city>q</city>

<state>q</state>

<country>Thailand</country>

</User>

</Users>

 

 

I've tried about a dozen different pieces of code found on the net, tried writing my own, all fail miserably.

 

What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/244664-xml-to-array/
Share on other sites

Try this:

<?php
      $doc = new DomDocument();
  $doc->validateOnParse = true;
      if(!$doc->load("users/users.xml")) {
	exit('Error occured!');
}

    $userList = $doc->getElementsByTagName('username');
$userCnt  = $userList->length;

for ($idx = 0; $idx < $userCnt; $idx++) {
    echo 'Username: ' .  $userList->item($idx)->nodeValue . "<br />";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/244664-xml-to-array/#findComment-1256853
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.