*ZUCHT*
Ik zeg toch al dat ik al weet hoe ik moet parsen, en dacht je nu echt dat ik niet al gegoogled had?
Stel je neemt een standaard tutorial die je via google altijd terug ziet komen
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| <?php
// The XML file that you wish to be parsed
$file = "xmltest.xml";
// This function tells the parser what to do with the data once it reaches the contents
// that appear between tags.
function contents($parser, $data){
echo $data;
}
// This function tells the parser to place a <b> where it finds a start tag.
function startTag($parser, $data){
echo "<b>";
}
// And this function tells the parser to replace the end tags with "<b><br />"
function endTag($parser, $data){
echo "</b><br />";
}
// These lines create the parser and then set the functions for the parser to use when
// reading the document.
$xml_parser = xml_parser_create();
// Sets the functions for start and end tags
xml_set_element_handler($xml_parser, "startTag", "endTag");
// Sets the function for the contents/data
xml_set_character_data_handler($xml_parser, "contents");
// Opens the file for reading
$fp = fopen($file, "r");
// Read the file and save its contents as the variable "data"
$data = fread($fp, 80000);
// This if statement does two things. 1) it parses the document according to our
// functions created above. 2) If the parse fails for some reason it returns an
// error message and also tells us which line the error occured at.
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
// Free the memory used to create the parser
xml_parser_free($xml_parser);
// Close the file when you're done reading it
fclose($fp);
?> |
met een vrij simpel xml :
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
| <?xml version="1.0"?>
<numbers>
<num>1</num>
<num>2</num>
<num>3</num>
<num>4</num>
<num>5</num>
<num>6</num>
<num>7</num>
<num>8</num>
<num>9</num>
<num>10</num>
</numbers> |
Ok, dit snap ik, maar ik wil meer. Ik wil dit geparsed hebben :
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
| <?xml version="1.0"?>
<numbers>
<num naam="klaas1">1</num>
<num naam="klaas2">2</num>
<num naam="klaas3">3</num>
<num naam="klaas4">4</num>
<num naam="klaas5">5</num>
<num naam="klaas6">6</num>
<num naam="klaas7">7</num>
<num naam="klaas8">8</num>
<num naam="klaas9">9</num>
<num naam="jan10">10</num>
</numbers> |
Dus hoe in die startTag functie zie ik een attribuut naam met een value?
[
Voor 16% gewijzigd door
Verwijderd op 06-08-2004 09:35
]