PHP XML DOM
�ڽ��� DOM ������ʹ�� PHP �д��� XML �ĵ���Ϊ���ܡ�
ʲô�� DOM��
W3C DOM �ṩ����� HTML �� XML �ĵ��ı������Լ����ڷ��ʺͲ�����Щ�ĵ��ı��ӿڡ�
W3C DOM ����Ϊ��ͬ�IJ��� (Core, XML �� HTML) �Ͳ�ͬ�ļ��� (DOM Level 1/2/3)��
- Core DOM - Ϊ�κνṹ���ĵ�������Ķ���
- XML DOM - Ϊ XML �ĵ�������Ķ���
- HTML DOM - Ϊ HTML �ĵ�������Ķ���
�����ϣ��ѧϰ�����й� XML DOM ��֪ʶ����������ǵ� XML DOM �̳���
XML ����
�����ȡ���� - �������������� - һ�� XML �ĵ�������Ҫ XML ��������
�����ֻ����� XML ���������ͣ�
- �������Ľ����������ֽ������� XML �ĵ�ת��Ϊ���ͽṹ����������ƪ�ĵ������ṩ�� API ���������ֵ�Ԫ�أ������ĵ�����ģ�� (DOM)��
- �����¼��Ľ��������� XML �ĵ���Ϊһϵ�е��¼�����ij��������¼�����ʱ������������ú�����������
DOM �������ǻ������Ľ�������
�뿴����� XML �ĵ�Ƭ�Σ�
<?xml version="1.0" encoding="ISO-8859-1"?> <from>John</from>
XML DOM �� XML ��Ϊһ�����νṹ��
- Level 1: XML �ĵ�
- Level 2: ��Ԫ��: <from>
- Level 3: �ı�Ԫ��: "John"
��װ
DOM XML ������������ PHP ���ĵ���ɲ��֡����谲װ�Ϳ���ʹ����Щ������
XML �ļ�
�������ǵ�������ʹ������� XML �ļ���
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
���غ���� XML
������Ҫ��ʼ�� XML ������������ XML�������������
����
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
print $xmlDoc->saveXML();
?>
���ϴ���������
George John Reminder Don't forget the meeting!
������������������в鿴Դ���룬�ῴ��������Щ HTML��
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
��������Ӵ�����һ�� DOMDocument-Object������ "note.xml" �е� XML ��������ĵ������С�
saveXML() �������ڲ� XML �ĵ�����һ���ַ������������ǾͿ����������
ѭ�� XML
����Ҫ��ʼ�� XML ������������ XML����ѭ�� <note> Ԫ�ص�����Ԫ�أ�
����
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
print $item->nodeName . " = " . $item->nodeValue . "<br />";
}
?>
���ϴ���������
#text = to = George #text = from = John #text = heading = Reminder #text = body = Don't forget the meeting! #text =
������������У���������ÿ��Ԫ��֮����ڿյ��ı��ڵ㡣
�� XML ����ʱ����ͨ�����ڽڵ�֮������հס�XML DOM �����������ǵ�����ͨ��Ԫ�أ��������ע�����ǣ���ʱ��������⡣
�����ϣ��ѧϰ�����й� XML DOM ��֪ʶ����������ǵ� XML DOM �̳���