สอบถามเกี่ยวกับ attribute ของ tag XML คะ

---ตัวอย่างไฟล์ xml---



<test>

  <family>

    <child marriage="3" bastard="0" protege="1" allchild="3">

      <marriage id="1">

        <cname>cccccc1</cname>

        <cpersonid>11111111</cpersonid>

        <cage>11</cage>

        <caddress>cccccc1</caddress>

      </marriage>

      <marriage id="2">

        <cname>cccccc2</cname>

        <cpersonid>2222222</cpersonid>

        <cage>22</cage>

        <caddress>ccccccc2</caddress>

      </marriage>

      <marriage id="3">

        <cname>cccccc3</cname>

        <cpersonid>333333</cpersonid>

        <cage>33</cage>

        <caddress>ccccccc3</caddress>

      </marriage>

      <protege id="1">

        <cname>aaaaaa1</cname>

        <cpersonid>4444444</cpersonid>

        <cage>44</cage>

        <caddress>aaaaaa1</caddress>

      </protege>

    </child>

  </family>

</test>



-----------------------------------------------------------------------------------------



1. ขอสอบถามนิดนึงคะว่าจะอ่านค่าของ child เพื่อนำเข้าฐาน mysql ต้องเขียนหรือต้องกำหนดยังไงบ้างคะ

2. ส่วนของ marriage id ถ้ามีมากกว่า 2 rows เขียนยังไงคะ ตอนนี้ใช้

   preg_match_all('/<cname>(.*?)<\/cname>/isu',$xml,$cname);

   echo 'cname 1 = '.$cname[1][0].'<br />';

   echo 'cname 2 = '.$cname[1][1].'<br />';



ขอบคุณล่วงหน้านะคะ smiley

16 ม.ค. 2555 3 2,233

วิธีที่ง่ายสุด น่าจะเป็นการใช้งานคำสั้งในกลุ่ม xml ของ PHP เอง



http://www.php.net/manual/en/function.xml-parse.php



ถ้าไม่ต้องการใช้คำสั่ง xml ของ PHP ลองดูเกี่ยวกับ RSS ครับ (คลิก "RSS" ในกรอบ Category ขวามมือ) จะมีฟังก์ชั่นที่ใช้งานกับ RSS ครับ ซึ่งก็ให้ดัดแปลง ให้เหมาะสมกับคำสั่งที่ต้องการนะครับ



ปล.ถ้าจะใช้ preg ลองคำสั่งนี้ครับ



'/(<child(.*)>(.*)<\/child>)/isu'





#1

ขอบคุณมากๆๆคะ
#2

คับ
#3
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^