xml用户数据库存在[重复]

This question already has an answer here:

i try to make an user database with xml. Now i don't know how to do this. This is my code

php:

<?php
session_start();


if($_SERVER['REQUEST_METHOD'] == 'POST'){
if($_POST['user'] == ""){
echo "this field may not empty";
}else{
$user = $_POST['user'];
}
?> 

How can i check if user is already exist in the xml file? And if username or userid is not exist create a new one

xml:

<users>
<user>
<userid>1</userid>
<username>usera</username>
</user>
<user>
<userid>2</userid>
<username>userb</username>
</user>
etc...
</users>

@fusion3k Thanks for the reaction. If i try this my output wil be "array":

$xml = simplexml_load_file("database/data.xml") or die('Can Not Load XML');
$usertest = $xml->xpath('//users/user[username[text()="usera"]]'); 
echo $usertest;
</div>

you can use an xpath query

$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;

$doc->load('users.xml');
$xpath->query("//username/text()='.$user.'")->length; //if exist the length is 1

see the documentation form more info http://php.net/manual/en/domxpath.query.php