![]() |
![]() ![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Zarejestrowani Postów: 230 Pomógł: 3 Dołączył: 8.01.2008 Ostrzeżenie: (10%) ![]() ![]() |
Parsując dokument XML natknąłem się na element zawierający w nazwie myślnik. Gdy zapiszę to w PHP tak:
Otrzymuje błąd: Cytat Parse error: syntax error, unexpected '-', expecting ',' or ')' Jak to można ominąć? -------------------- http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
|
|
|
![]()
Post
#2
|
|
![]() Grupa: Przyjaciele php.pl Postów: 5 724 Pomógł: 259 Dołączył: 13.04.2004 Skąd: N/A Ostrzeżenie: (0%) ![]() ![]() |
$obiekt->{'element-obiektu'}
badz wsadz go do ArrayObject i potem $ao = new ArrayObject($obiekt); $ao['element-obiektu'] -------------------- Nie lubię jednorożców.
|
|
|
![]()
Post
#3
|
|
Grupa: Zarejestrowani Postów: 230 Pomógł: 3 Dołączył: 8.01.2008 Ostrzeżenie: (10%) ![]() ![]() |
Dzięki, działa! :-)
Tylko teraz mam następny problem - w tym pliku XML mam kilka elementów o takiej samej nazwie - tj. w danym elemencie jest sześć elementów o takiej samej nazwie. Jak pobiorę je normalnie $element->podelement, pobiera mi tylko ostatni. Jak mogę pobrać je wszystkie? XML parsuje poprzez simplexml. Jak nie da rady nic z tym, to najwyżej, tak jak radził bonzo, przerobie ten obiekt na tablice :-) -------------------- http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
|
|
|
![]()
Post
#4
|
|
![]() Grupa: Przyjaciele php.pl Postów: 5 724 Pomógł: 259 Dołączył: 13.04.2004 Skąd: N/A Ostrzeżenie: (0%) ![]() ![]() |
Masz w manualu pokazane jak sie do nich dobierac.
Po prostu foreach ( $xml->podelement as $el ) >.. -------------------- Nie lubię jednorożców.
|
|
|
![]()
Post
#5
|
|
Grupa: Zarejestrowani Postów: 230 Pomógł: 3 Dołączył: 8.01.2008 Ostrzeżenie: (10%) ![]() ![]() |
I tak właśnie robię. Mam taki obiekt:
Cytat SimpleXMLElement Object ( [staff-type] => alternate [staff-lines] => 6 [staff-tuning] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 1 ) [tuning-step] => E [tuning-octave] => 4 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 2 ) [tuning-step] => B [tuning-octave] => 3 ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 3 ) [tuning-step] => G [tuning-octave] => 3 ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 4 ) [tuning-step] => D [tuning-octave] => 3 ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 5 ) [tuning-step] => A [tuning-octave] => 2 ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 6 ) [tuning-step] => D [tuning-octave] => 2 ) ) ) I chce się dobrać do tej tablicy "staff-tuning". Jeżeli zrobię poprzez $obiekt->{'staff-tuning'}, pobiera mi tylko ostatni element. Co robię nie tak? -------------------- http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
|
|
|
![]()
Post
#6
|
|
![]() Grupa: Przyjaciele php.pl Postów: 5 724 Pomógł: 259 Dołączył: 13.04.2004 Skąd: N/A Ostrzeżenie: (0%) ![]() ![]() |
Nieczytelny przyklad.
-------------------- Nie lubię jednorożców.
|
|
|
![]()
Post
#7
|
|
Grupa: Zarejestrowani Postów: 230 Pomógł: 3 Dołączył: 8.01.2008 Ostrzeżenie: (10%) ![]() ![]() |
Cytat SimpleXMLElement Object ( [staff-type] => alternate [staff-lines] => 6 [staff-tuning] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 1 ) [tuning-step] => E [tuning-octave] => 4 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 2 ) [tuning-step] => B [tuning-octave] => 3 ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 3 ) [tuning-step] => G [tuning-octave] => 3 ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 4 ) [tuning-step] => D [tuning-octave] => 3 ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 5 ) [tuning-step] => A [tuning-octave] => 2 ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 6 ) [tuning-step] => D [tuning-octave] => 2 ) ) ) o, teraz to czytelniej wygląda :-) -------------------- http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
|
|
|
![]()
Post
#8
|
|
Grupa: Zarejestrowani Postów: 161 Pomógł: 9 Dołączył: 14.07.2008 Ostrzeżenie: (0%) ![]() ![]() |
Musisz zrobić pętle w pętli. Podam Ci przykład z książki:
-------------------- |
|
|
![]()
Post
#9
|
|
Grupa: Zarejestrowani Postów: 230 Pomógł: 3 Dołączył: 8.01.2008 Ostrzeżenie: (10%) ![]() ![]() |
marcinpruciak -> robiłem to właśnie tak, jak przedstawiłeś
Już działa, błąd tkwił w czym innym - próbowałem jako klucze tablicy dawać wynik getAttribute - a to zwraca obiekt :-) Po przekonwertowaniu tego obiketu-atrybutu na int wszystko ładnie działa :-) -------------------- http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
|
|
|
![]() ![]() |
![]() |
Wersja Lo-Fi | Aktualny czas: 14.06.2025 - 18:13 |