[PHP] XML Parsing

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Hoi,

Ben eens bezig te kijken hoe je XML parst met PHP. Ben wel zover dat ik met die expat functies dus gewoon xml in kan lezen enzo.

Heb alleen nog een vraagje. Stel ik heb de volgende xml :

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
<CATEGORY NAME="Cat 1">
    <SUBCATECORY NAME="Sub 1">
        <PRODUCT ID="1234" NAME="Product 1">
            <KLEUR>rood</KLEUR>
            <PRIJS>5</KLEUR>
        </PRODUCT>
        <PRODUCT ID="8394" NAME="Product 2">
            <KLEUR>wit</KLEUR>
            <PRIJS>5</KLEUR>
            <GEWICHT>50 kg</GEWICHT>
        </PRODUCT>      
    </SUBCATECORY>
</CATEGORY>


Hoe krijg ik de value van vb CATEGORY en dan de NAME. Dus hoe krijg ik de waarde "Cat 1".

Acties:
  • 0 Henk 'm!

  • curry684
  • Registratie: Juni 2000
  • Laatst online: 06-09 00:37

curry684

left part of the evil twins

Even een stomme vraag maar: wat heb je al geprobeerd? :?

* curry684 wijst subtiel richting P&W FAQ - De "quickstart"

Professionele website nodig?


Acties:
  • 0 Henk 'm!

  • Zoijar
  • Registratie: September 2001
  • Niet online

Zoijar

Because he doesn't row...

[google=xml tutorial php] ...

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
*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 ]


Acties:
  • 0 Henk 'm!

  • curry684
  • Registratie: Juni 2000
  • Laatst online: 06-09 00:37

curry684

left part of the evil twins

Verwijderd schreef op 06 augustus 2004 @ 09:28:
*ZUCHT*

Ik zeg toch al dat ik al weet ho eik moet parsen, en dacht je nu echt dat ik niet al gegoogled had?
Ja, als jij niet vertelt wat je al gedaan hebt gaan we ervan uit dat je niets gedaan hebt. Dat krijg je met een zo karige topicstart als je nu geproduceerd hebt. De simpele regel 'vertel wat je al gedaan hebt' is niet om jou werk te bezorgen, maar om te voorkomen dat andere mensen het voor je moeten doen terwijl het niet nodig is. Een iets andere houding dan dit zou je dus niet misstaan :/

Professionele website nodig?


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
misschien "Ben wel zover dat ik met die expat functies dus gewoon xml in kan lezen enzo." ??? Dus heb al wat gedaan. Ik heb met de standaard expat functies die php aanlevert een xml ingelezen. Dus wat bedoel je met "ga ervan uit dat er niets gedaan is?"

[ Voor 49% gewijzigd door Verwijderd op 06-08-2004 09:40 ]


Acties:
  • 0 Henk 'm!

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 02:21

Janoz

Moderator Devschuur®

!litemod

Nou, je hebt iig niet in de handleiding gekeken, anders was je er wel uitgekomen ;). (Hint: Hetgeen jij wil lezen is een attribute)

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
aaaaah

kiek .. thnx .. ga vanaf daar weer verder .. thnx ...
Pagina: 1