Solved

XML parser

Posted on 2004-10-10
3
240 Views
Last Modified: 2013-11-19
Hi all,
        Can anyone help me about this how do I parse a xml file using php?  for example i need a attribute named "USER" in the xml file.  How do I get this value from an xml and use it in an php file?

Thanks alot to all
!!
0
Comment
Question by:Kaiwap
  • 2
3 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 12272837
See: http://www.php.net/manual/en/function.xml-parse-into-struct.php
In the comments there are a lot of examples how to do this or how to get a strcuture wich can be searched /accessed easyer
0
 

Author Comment

by:Kaiwap
ID: 12289563
I am sorry but i m not very good in this script.
for example i have
<?xml version = "1.0" encoding="Windows-874" ?>

      <user>user</user>
      <password>pwd</password>
                <filename>picture.jpg</filename>
      <content>albkja;dlkjoasdihfashdf;kj;eifjo;eijf</content>

if i only want the values in the filename and contents and show it on the other page
Can you give me a sample of how it is done?  
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 250 total points
ID: 12290965
Have a look at this example:

<?php

$data = '<?xml version = "1.0" encoding="Windows-874" ?>
<tag>
     <user>user</user>
     <password>pwd</password>
                <filename>picture.jpg</filename>
     <content>albkja;dlkjoasdihfashdf;kj;eifjo;eijf</content>
     <user>user</user>
     <password>pwd</password>
                <filename>picture2.jpg</filename>
     <content>albkja;dlkjoasdihfassdfdshdf;kj;eifjo;eijf</content>
</tag>
';

$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$result = array();
foreach($index['FILENAME'] as $num => $id) {
    $result[] = array(
        'filename' => $vals[$id]['value'],
        'content'  => $vals[$index['CONTENT'][$num]]['value'],
        );
}

// for debugging/understanding the structures:
var_dump($vals);
var_dump($index);
var_dump($result);

$result will contain:
Array
(
    [0] => Array
        (
            [filename] => picture.jpg
            [content] => albkja;dlkjoasdihfashdf;kj;eifjo;eijf
        )

    [1] => Array
        (
            [filename] => picture2.jpg
            [content] => albkja;dlkjoasdihfassdfdshdf;kj;eifjo;eijf
        )

)

This will only work correctly if you have the same count of fielname and contents tag and if the are in the same xml-level.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now