开发者

PHP & XML parsing

I have searched everything on google and still i cant seems to find a solution for this. Basically im helping my friend to create a php frontend for his ftp.

The ftp details are saved in xml. So i have to parse it to a php.

<FileZillaServer>
<Users>
<User Name="anonymous">
<Option Name="Pass">aaaaaa</Option>
<Option Name="Group"/>
<Option Name="Bypass server userlimit">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="IP Limit">0</Option>
<Option Name="Enabled">1</Option>
<Option Name="Comments"/>
<Option Name="ForceSsl">0</Option>
<IpFilter>
<Disallowed/>
<Allowed/>
</IpFilter>
<Permissions>
<Permission Dir="C:\xampp\anonymous">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">0</Option>
<Option Name="FileDelete">0</Option>
<Option Name="FileAppend">0</Optio开发者_开发问答n>
<Option Name="DirCreate">0</Option>
<Option Name="DirDelete">0</Option>
<Option Name="DirList">1</Option>
<Option Name="DirSubdirs">0</Option>
<Option Name="IsHome">1</Option>
<Option Name="AutoCreate">0</Option>
</Permission>
<Permission Dir="C:\xampp\anonymous\incoming">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">1</Option>
<Option Name="FileDelete">0</Option>
<Option Name="FileAppend">0</Option>
<Option Name="DirCreate">0</Option>
<Option Name="DirDelete">0</Option>
<Option Name="DirList">1</Option>
<Option Name="DirSubdirs">0</Option>
<Option Name="IsHome">0</Option>
<Option Name="AutoCreate">0</Option>
</Permission>
</Permissions>
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="0" UlType="0" UlLimit="10" ServerUlLimitBypass="0">
<Download/>
<Upload/>
</SpeedLimits>
</User>
</Users>
</FileZillaServer>

Basically above is a sample content from XML. I need to know how to parse the content and display in PHP.

Just to display Name and pass.

Thank you


There are several PHP XML handling modules that you can use. DOMDocument is a good one if you are dealing with valid XML, which you appear to be.

The following code will parse your document and output the user name and password found within each <User> tag.

$doc = new DOMDocument();
$doc->load('test.xml');
$userNodes = $doc->getElementsByTagName('Users');
foreach($userNodes as $user) {
  foreach($user->childNodes as $userData) {
    if ($userData->nodeName == 'User') {
      echo "User name: ";
      echo $userData->attributes->getNamedItem('Name')->nodeValue;
      foreach($userData->childNodes as $n) {
        if ($n->nodeName == 'Option' && $n->attributes->getNamedItem('Name')->nodeValue == 'Pass') {
          echo " Password: ".$n->nodeValue."\n";
          break;
        }
      }
    }
  }
}


$xml = "your xml string";
$xml_object = simplexml_load_string($xml);

or

$xml_file = "path/to/file";
$xml_object = simplexml_load_file($xml_file);

Read up on accessing the data through the resulting XML Object here: http://www.php.net/manual/en/book.simplexml.php


You can use this simple code to extract your values.

$xml =  simplexml_load_string(file_get_contents('xml.xml'));

//now extract all the variables are extracted below
$i = 0;

foreach($xml->Users->User as $key=>$myuser) {
    echo 'Name: '.$myuser->Name.'<br />';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜