Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php,xml] Dodawanie atrybutu..
JeFF
post
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 16.06.2008

Ostrzeżenie: (0%)
-----


test.xml :
Kod
<?xml version="1.0"?>
     <ids>
         <id>One</id>
         <id type="2">Two</id>
         <id type="3">Three</id>
         <id type="4">Four</id>
         <id type="5">Five</id>
         <id type="6">Six</id>
     </ids>


php:
Kod
<?php
     $xml = simplexml_load_file("test.xml");
     $xml->id[0]->addAttribute("type", "1");
     $xml->asXML("test.xml");
     ?>


wynik xml'a :
Kod
<?xml version="1.0"?>
       <ids>
           <id type="1">One</id>
           <id type="2">Two</id>
           <id type="3">Three</id>
           <id type="4">Four</id>
           <id type="5">Five</id>
           <id type="6">Six</id>
       </ids>


i nie wiem jak to przerobić żeby do takiego xml'a:
Kod
<?xml version="1.0"?>
     <ad num="126" type="0" time="6900">
         <ids>
             <id type="1"/>
             <id type="2"/>
         </ids>
     </ad>


Dodało atrybut (web) w tym miejscu :
Kod
<?xml version="1.0"?>
       <ad num="126" type="0" time="6900" web="">
           <ids>
               <id type="1"/>
               <id type="2"/>
           </ids>
       </ad>


Ciagle jak prubuje coś zrobić wyrzuca mi błąd :
Fatal error
: Call to a member function addAttribute() on a non-object in

Prosze o pomoc..
Go to the top of the page
+Quote Post
splatch
post
Post #2





Grupa: Zarejestrowani
Postów: 487
Pomógł: 7
Dołączył: 7.01.2004
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Czy próbujesz w ten sposób?
  1. <?php
  2.     $xml = simplexml_load_file("test.xml");
  3.     $xml->addAttribute("web", "");
  4.     $xml->asXML("test.xml");
  5. ?>


--------------------
Łukasz Dywicki
Independent Java and open source software consultant.
Blog - Java, OSGi, integracja oprogramowania..
Go to the top of the page
+Quote Post
JeFF
post
Post #3





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 16.06.2008

Ostrzeżenie: (0%)
-----


Próbowałem w ten sposób :
Kod
<?php
$xml = simplexml_load_file("test.xml");
$xml->ad[0]->addAttribute("web", "");
$xml->asXML("test.xml");
?>


Dzięki, to co podałeś działa.. biggrin.gif

Mam jeszcze jedno pytanie, mianowicie jak z tego xml'a :
Kod
<?xml version="1.0"?>
       <ad num="126" type="0" time="6900" web="">
           <ids>
               <id type="1"/>
               <id type="2"/>
           </ids>
       </ad>


Za pomocą php wyświetlić
Kod
               <id type="1"/>
               <id type="2"/>

W postaci :
Kod
  1
  2
Go to the top of the page
+Quote Post
splatch
post
Post #4





Grupa: Zarejestrowani
Postów: 487
Pomógł: 7
Dołączył: 7.01.2004
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Zgaduję że to może być:
  1. <?php
  2.    $xml = simplexml_load_file("test.xml");
  3.  
  4.    foreach ($xml->ids->id as $id) { echo $id->type; }
  5.    
  6. ?>


--------------------
Łukasz Dywicki
Independent Java and open source software consultant.
Blog - Java, OSGi, integracja oprogramowania..
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 03:22