|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
学习数据库了,MYSQL可算是PHP的黄金搭档了,不过,虽然话是这么说,你也可能恨不得把MYSQL给生吞活剥了,因为这一行一列的东东简直让自己头晕目眩。1、媒介 XML树状条理布局光显,十分合适作为设置文件。PHP中可使用DOMXML来操纵XML。本文总结一下PHP利用DOMXML创立、增加节点、查询XML文件。2、利用DOMXML XML中节点分为元素和文本,DOMDocument范例是文档范例,供应了操纵元素和文本的成员函数和属性。DOMDocument类以下:复制代码DOMDocumentextendsDOMNode{/*Properties*/readonlypublicstring$actualEncoding;readonlypublicDOMConfiguration$config;readonlypublicDOMDocumentType$doctype;readonlypublicDOMElement$documentElement;publicstring$documentURI;publicstring$encoding;publicbool$formatOutput;readonlypublicDOMImplementation$implementation;publicbool$preserveWhiteSpace=true;publicbool$recover;publicbool$resolveExternals;publicbool$standalone;publicbool$strictErrorChecking=true;publicbool$substituteEntities;publicbool$validateOnParse=false;publicstring$version;readonlypublicstring$xmlEncoding;publicbool$xmlStandalone;publicstring$xmlVersion;/*Methods*/__construct([string$version[,string$encoding]])DOMAttrcreateAttribute(string$name)DOMAttrcreateAttributeNS(string$namespaceURI,string$qualifiedName)DOMCDATASectioncreateCDATASection(string$data)DOMCommentcreateComment(string$data)DOMDocumentFragmentcreateDocumentFragment(void)DOMElementcreateElement(string$name[,string$value])DOMElementcreateElementNS(string$namespaceURI,string$qualifiedName[,string$value])DOMEntityReferencecreateEntityReference(string$name)DOMProcessingInstructioncreateProcessingInstruction(string$target[,string$data])DOMTextcreateTextNode(string$content)DOMElementgetElementById(string$elementId)DOMNodeListgetElementsByTagName(string$name)DOMNodeListgetElementsByTagNameNS(string$namespaceURI,string$localName)DOMNodeimportNode(DOMNode$importedNode[,bool$deep])mixedload(string$filename[,int$options=0])boolloadHTML(string$source)boolloadHTMLFile(string$filename)mixedloadXML(string$source[,int$options=0])voidnormalizeDocument(void)boolregisterNodeClass(string$baseclass,string$extendedclass)boolrelaxNGValidate(string$filename)boolrelaxNGValidateSource(string$source)intsave(string$filename[,int$options])stringsaveHTML(void)intsaveHTMLFile(string$filename)stringsaveXML([DOMNode$node[,int$options]])boolschemaValidate(string$filename)boolschemaValidateSource(string$source)boolvalidate(void)intxinclude([int$options])/*Inheritedmethods*/DOMNodeDOMNode::appendChild(DOMNode$newnode)DOMNodeDOMNode::cloneNode([bool$deep])boolDOMNode::hasAttributes(void)boolDOMNode::hasChildNodes(void)DOMNodeDOMNode::insertBefore(DOMNode$newnode[,DOMNode$refnode])boolDOMNode::isDefaultNamespace(string$namespaceURI)boolDOMNode::isSameNode(DOMNode$node)boolDOMNode::isSupported(string$feature,string$version)stringDOMNode::lookupNamespaceURI(string$prefix)stringDOMNode::lookupPrefix(string$namespaceURI)voidDOMNode::normalize(void)DOMNodeDOMNode::removeChild(DOMNode$oldnode)DOMNodeDOMNode::replaceChild(DOMNode$newnode,DOMNode$oldnode)}复制代码元素节点类DOMElement范例界说以下:复制代码1DOMElementextendsDOMNode{2/*Properties*/3readonlypublicbool$schemaTypeInfo;4readonlypublicstring$tagName;5/*Methods*/6__construct(string$name[,string$value[,string$namespaceURI]])7stringgetAttribute(string$name)8DOMAttrgetAttributeNode(string$name)9DOMAttrgetAttributeNodeNS(string$namespaceURI,string$localName)10stringgetAttributeNS(string$namespaceURI,string$localName)11DOMNodeListgetElementsByTagName(string$name)12DOMNodeListgetElementsByTagNameNS(string$namespaceURI,string$localName)13boolhasAttribute(string$name)14boolhasAttributeNS(string$namespaceURI,string$localName)15boolremoveAttribute(string$name)16boolremoveAttributeNode(DOMAttr$oldnode)17boolremoveAttributeNS(string$namespaceURI,string$localName)18DOMAttrsetAttribute(string$name,string$value)19DOMAttrsetAttributeNode(DOMAttr$attr)20DOMAttrsetAttributeNodeNS(DOMAttr$attr)21voidsetAttributeNS(string$namespaceURI,string$qualifiedName,string$value)22voidsetIdAttribute(string$name,bool$isId)23voidsetIdAttributeNode(DOMAttr$attr,bool$isId)24voidsetIdAttributeNS(string$namespaceURI,string$localName,bool$isId)25/*Inheritedmethods*/26DOMNodeDOMNode::appendChild(DOMNode$newnode)27DOMNodeDOMNode::cloneNode([bool$deep])28boolDOMNode::hasAttributes(void)29boolDOMNode::hasChildNodes(void)30DOMNodeDOMNode::insertBefore(DOMNode$newnode[,DOMNode$refnode])31boolDOMNode::isDefaultNamespace(string$namespaceURI)32boolDOMNode::isSameNode(DOMNode$node)33boolDOMNode::isSupported(string$feature,string$version)34stringDOMNode::lookupNamespaceURI(string$prefix)35stringDOMNode::lookupPrefix(string$namespaceURI)36voidDOMNode::normalize(void)37DOMNodeDOMNode::removeChild(DOMNode$oldnode)38DOMNodeDOMNode::replaceChild(DOMNode$newnode,DOMNode$oldnode)39}复制代码文本范例DOMText界说以下:复制代码DOMTextextendsDOMCharacterData{/*Properties*/readonlypublicstring$wholeText;/*Methods*/__construct([string$value])boolisWhitespaceInElementContent(void)DOMTextsplitText(int$offset)/*Inheritedmethods*/voidDOMCharacterData::appendData(string$data)voidDOMCharacterData::deleteData(int$offset,int$count)voidDOMCharacterData::insertData(int$offset,string$data)voidDOMCharacterData::replaceData(int$offset,int$count,string$data)stringDOMCharacterData::substringData(int$offset,int$count)}复制代码DOMNode节点界说以下:复制代码DOMNode{/*Properties*/publicreadonlystring$nodeName;publicstring$nodeValue;publicreadonlyint$nodeType;publicreadonlyDOMNode$parentNode;publicreadonlyDOMNodeList$childNodes;publicreadonlyDOMNode$firstChild;publicreadonlyDOMNode$lastChild;publicreadonlyDOMNode$previousSibling;publicreadonlyDOMNode$nextSibling;publicreadonlyDOMNamedNodeMap$attributes;publicreadonlyDOMDocument$ownerDocument;publicreadonlystring$namespaceURI;publicstring$prefix;publicreadonlystring$localName;publicreadonlystring$baseURI;publicstring$textContent;/*Methods*/DOMNodeappendChild(DOMNode$newnode)DOMNodecloneNode([bool$deep])boolhasAttributes(void)boolhasChildNodes(void)DOMNodeinsertBefore(DOMNode$newnode[,DOMNode$refnode])boolisDefaultNamespace(string$namespaceURI)boolisSameNode(DOMNode$node)boolisSupported(string$feature,string$version)stringlookupNamespaceURI(string$prefix)stringlookupPrefix(string$namespaceURI)voidnormalize(void)DOMNoderemoveChild(DOMNode$oldnode)DOMNodereplaceChild(DOMNode$newnode,DOMNode$oldnode)}复制代码3、测试程序复制代码1<?php23constINDEX_FILE_NAME="student_file_index.xml";45//文件索引类6classfile_index7{8publicfunctionset_file_index($file_name,$cur_count,$total_count)9{10$this->file_name=$file_name;11$this->cur_count=$cur_count;12$this->total_count=$total_count;13}14publicfunctionget_file_name()15{16return$this->file_name;17}18publicfunctionget_cur_count()19{20return$this->cur_count;21}22publicfunctionget_total_count()23{24return$this->total_count;25}2627private$file_name;//文件名28private$cur_count;//以后纪录数29private$total_count;//总纪录数30}3132functioncreate_file_index(array$params)33{34$index=newfile_index();35$index->set_file_index($params[file_name],36$params[cur_count],$params[total_count]);37return$index;38}3940functioncreate_file_node(DOMDocument$doc,file_index$index)41{42//创立一个file元素43$file=$doc->createElement("file");44//创立一个属性元素45$name_attr=$doc->createAttribute("name");46//将该属性增加到file元素上47$file->appendChild($name_attr);4849//创立一个文本元素50$file_name=$doc->createTextNode($index->get_file_name());51//将文本元素增加name_attr属性上52$name_attr->appendChild($file_name);5354//创立一个cur_count元素55$cur_count=$doc->createElement("cur_count",strval($index->get_cur_count()));56//将cur_count增加到file元素下57$cur_count=$file->appendChild($cur_count);5859//创立一个total_count元素60$total_count=$doc->createElement("total_count",61strval($index->get_total_count()));62//将total_count增加到file元素下63$total_count=$file->appendChild($total_count);6465return$file;66}6768functioncreate_index_file($index_file_name,array$params)69{70//创立一个文档71$doc=newDOMDocument("1.0","utf-8");72//创立根元素73$root=$doc->createElement("index");74$root=$doc->appendChild($root);7576//创立一个索引布局77$index=create_file_index($params);78$file=create_file_node($doc,$index);7980//将file看到根元素下81$root->appendChild($file);82$doc->save($index_file_name);83returntrue;84}8586functionadd_index_file($index_file_name,array$params)87{88//创立一个文档89$doc=newDOMDocument();90//加载xml文件91$doc->load($index_file_name);92//猎取index元素列表93$node_list=$doc->getElementsByTagName(index);94//猎取根元素95$root=$node_list->item(0);96//创立一个索引布局97$index=create_file_index($params);98$file=create_file_node($doc,$index);99//将file看到根元素下100$root->appendChild($file);101$doc->save($index_file_name);102}103104functiontraverse_file_index($index_file_name)105{106$file_index=array();107$doc=newDOMDocument();108$doc->load($index_file_name);109//猎取file元素汇合110$file_list=$doc->getElementsByTagName(file);111//猎取cur_count元素汇合112$cur_count_list=$doc->getElementsByTagName(cur_count);113//猎取total_count元素汇合114$total_count_list=$doc->getElementsByTagName(total_count);115for($i=0;$i<$file_list->length;$i++){116$index=newfile_index();117//猎取file元素的name属性值118$file_name=$file_list->item($i)->attributes->getNamedItem("name")->nodeValue;119$index->set_file_index($file_name,$cur_count_list->item($i)->nodeValue,120$total_count_list->item($i)->nodeValue);121$file_index[$i]=$index;122}123124return$file_index;125}126127/*****************fortest*********************/128$params=array();129$index_file_name=INDEX_FILE_NAME;130131if(file_exists($index_file_name)){132$params[file_name]="student_info_2014_02_12";133$params[cur_count]=10;134$params[total_count]=10;135echo"Addindextofile.
";136add_index_file($index_file_name,$params);137}138else{139$params[file_name]="student_info_2014_02_11";140$params[cur_count]=23;141$params[total_count]=33;142echo"Createindexfile.
";143create_index_file($index_file_name,$params);144}145146//测试读取xm文件147echo"Readindexcontentfrom".$index_file_name."
";148echo"file_name cur_count total_count.
";149$file_index=traverse_file_index($index_file_name);150foreach($file_indexas$index){151echo$index->get_file_name();152echo" ";153echo$index->get_cur_count();154echostrval($cur_count);155echo" ";156echo$index->get_total_count();157echo"
";158}终于学会把表单的数据插入数据库,然后显示出来了,应该说一个程序的雏形已经诞生了。 |
|