때로는 XML을 단순히 저장하거나 일부 구성과 같은 일부 소규모 응용 프로그램으로 만 사용하면 XML을 직접 읽고 작성하면 효율성이 우선 순위입니다. Delphi 박스에는 직접 읽기 및 쓰기 XML 파일 (예제 및 코드)이 있으며 핵심 기능은 다음 두 기능입니다 (1 개의 읽기 및 1 개의 쓰기).
{--------------------------------------------------------- --------------------------------- 재미/pro : getxmlnodevalue@date : 2004.12.11@param : xmlfilexml 文件 @param : xmlnodepath 노드 @Param : XMLARATTRNAME NODE의 속성 이름을 직접 가져 오면이 매개 변수를 무시할 수 있습니다. @Param : DEP 노드의 매개 변수의 구분 기 기본값은@return : 첫 번째 노드의 값 -------------------------------- --------------------------------------------------------- --------------------------------------------------------- --------------------------------------------------------- -------------------------------------------- -------------------------------------------- } functionGetXmlnodevalue (StrentityEngineFile : String; xmlnodepath : String; constxmlattrName : String = '; constdep : char ='. ') : string; varxmldocument : ixmldocument; node : ixmlnode; xmlnodelist : i : integer; intelger; integer; // XML 노드 경로 XMLNODELIST : = TSTRINGLIST.CREATE; XMLNODELIST.DELIMITER : = DEP; XMLNODELIST.DELIDETEDTEXT : = XMLNODEPATH; urlCount : = xmlnodelist.count; // xml xmldocument : = txmldocuml (niLdocuml); OMFILE (StrentityEngineFile); xmlDocument.Active : = true; trynode : = xmldocument.documentElement; if (node.nodename = xmlnodelist [0]) 그런 다음 node fori : = 1tourlcount-1dobeginif (node <> nil)를 스캔합니다 (node.childnodes, xmlnodelist [i]) elsebreak; end; end; if (node = nil) thebeginresult : = ''; endelsebegin // 속성 또는 노드 컨텐츠를 취할 것인지 (trim (xmlattrname) = ') thenResult : = node.textelseresult : = node.attributeenodes.nodes [xmlattrname] .nodevalue; end; endselsebeginresult : = ''; end; exceptresult : = 'error'; end; xmldocument.active : = false; end; {----- --------------------------------------------------------- -------------------------------- Fun/Pro : Setxmlnodevalue@date : 2004.12.11@param : xmlfilexml 파일@param : xmlnodepath node@param : xmlattrname 노드의 속성 이름은 노드 값을 직접 가져 오면이 매개 변수를 무시할 수 있습니다. @Param : DEP 노드의 매개 변수의 구분 기자, 기본값@return : 작동하지 않음 ---------------------------------------------------- --------------------------------------------------------- --------------------------------------------------------- --------------------------------------------------------- -------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------- OOIOOOOOOOOUOUOUOOD색 어션tOIOSTXMLNODEVALUE (functionstxmlnodevalue) StrentityEngineFile : String; xmlnodepath : String; constxmlattrName : string = ''; constValue : string = ''; constdep : char = '.') : boolean; varxmldocument : ixmldocument; node : ixmlnode; xmlnodelist : i : urlcount; : Integer; 시작 // XML 노드 경로 XMLNODELIST : = tstringList.create; xmlnodelist.delimiter : = dep; xmlnodelist.delimitedText : = xmlnodepath; urlcount : = xmlnodelist.count; // xmldocument : = txmldocument. ; xmldocument.loadfromFile (StrentityEngineFile); xmldocument.Active : = true; true; trynode : = xmldocument.documentElement; if (node.nodename = xmlnodelist [0]) thenbegin // 스캔 노드 포리 : = 1Tourlcount-1dobeginif (node <> node) whenNode : = getNodefromixMlnodelist (node.childnodes, xmlnodelist [i]) elsebreak; end; if (node <> nil) thebeginif (trim (xmlattrname) = '') thatnode.text : = valueelseNode.attributeNodes.Nodes [xmlattrname] : = value; xmldocument.savetofile (StrentityEngineFile); end; end; 결과 : = true; exceptresult : = false; xmldocument.active : = false; end;그러나 위의 두 기능에는 문제가 있습니다. 노드 이름과 속성 이름으로 첫 번째 레코드 만 찾을 수 있습니다. 예를 들어 : 다음과 유사한 XML 파일을 조작하려면 동일한 속성 이름을 가진 여러 노드와 속성 이름이 있지만 속성의 값은 다르고 위의 읽기 및 쓰기 기능은 계속됩니다. 스트라이크.
<colourname = normalAttRipted = 100Green = 125Blue = 150/> <colourname = goodAttributed = 150green = 175blue = 200/> <colourname = advoliteAttributed = 0green = 0Blue = 255/>좋아, 프로그래머들에게 가장 큰 즐거움은 스스로하는 것입니다. 이 두 기능을 수정합시다. 원래 함수에 두 개의 매개 변수가 추가됩니다.
{--------------------------------------------------------- ----------------------------------------------------------- Fun/Pro : getxmlnodespecialvalue@date : 2004.12.11@param : xmlfilexml 파일@param : xmlnodepath node @param : xmlarattrname node 값을 직접 사용하면이 매개 변수를 무시할 수 있습니다. @Param : XMLSpecialName @Param에 의해 검색 될 노드의 속성 이름 : XMLSpecialValue @Param : DEP Node의 매개 변수 분리기를 검색 할 노드의 특정 속성에 해당하는 값입니다. @value : value of the value. 특정 속성 ----------------------------------------- ------ -------------------------} functionGetXmlNodEspecialValue (StrentityEngineFile : String; XmlNodePath : String; constxmlattrName : String = ''; constxmlSpecialName : string = ''; constxmlspecialValue : string = ''; constdep : char = '.') : String; varxmldocument : ixmldocument; node : ixmlnode; xmlnodelist : tstrings; i : Integer; urlcount; integer; xmlnode 경로 xmlnodeList : = tstringlist.create; xmlnodelist.delimiter : = dep; xmlnodelist.delimitedText : = xmlnodepath; urlcount : = xmlnodelist.count; // xml 객체 xmldocument : = txmldocument.create (nil); xmldocument. ldocument. active : = true; trynode : = xmldocument.documentElement; if (node.nodename = xmlnodelist [0]) thenbegin // node fori : = 1tourlcount-1dobeginif (node <> nil) thenbeginnode : = getNodefromixmlnodelist (node.child.childnodeDes, xmlednoder [i]); endLeSebreak; end; if (node = nil) theBeginResult : = ''; endelSebegin // 속성 또는 노드 컨텐츠를 취할 것인지 if (trim (xmlarattrName) = '') theResult : = node.TexTelSebeginresult : = node.attributeNodes.nodes [xmlSpecialName] .nodevalue; // 여기서 임시 변수를 선언하고 싶지 않으므로 결과를 사용하여 숨겨진 위험이있을 수 있습니다. (result <mlspecialvalue)) dobeginnode : = node.nextsibling (node.nodename = '#comment') dobeginnode; 끝; 결과 : = node.attributeNodes.Nodes [XMLATTRNAME] .NODEVALUE; END; END; END; END; ENDELSEBEGINRESULT : = ''; END; EXCEPTRESULT : = 'ERROR'; END; XMLDocument.Active : = FALLE; END;기능을 작성하십시오
{--------------------------------------------------------- ----------------------------------------- Fun/Pro : SetxmlNodespecialValue@date : 2004.12 .11@param : xmlfilexml file@param : xmlnodepath node @param : xmlarattrname 속성 이름은 노드 값을 직접 가져 오면이 매개 변수를 무시할 수 있습니다. @Param : XMLSPECIALNAME @Param에 의해 검색 될 노드의 속성 이름 : XMLSpecialValue @Param : DEP Node 매개 변수 분리기에 의해 검색 될 노드의 속성에 해당하는 값. 성공 여부 -------------------------------------------- ------ ------------------------} functionsetxmlnodespecialValue (StrentityEngineFile : String; XmlNodePath : String; constxmlattrName : String = ''; constValue : string = ''; constxmlspecialName : string = ''; constxmlspecialValue : string = ''; constdep : char = '.') : boolean; varxmldocument : ixmldocument; node : ixmlnode; xmlnodelist : tstrings; i : urlcount : integer : integer ; cmpValue : string; 시작 // xml 노드 경로 xmlnodelist : = tstringlist.create; xmlnodelist.delimiter : = = dep; xmlnodelist.delimitedText : = xmlnodePath; urlcount : = xmlnodelist.count; // xmlocument xmldocument (xmldocument). nil); xmldocument.loadfromFile (StrentityEngineFile); xmldocument.Active : = true; trynode : = xmldocument.documentElement; if (node.nodename = xmlnodelist [0]) thenbegin // scan node fori : = 1tourlcount-1dobeginif (node <) nil) thenNode : = getNodefromixMlNodelist (node.childnodes, xmlnodelist [i]) elsebreak; end; end; if (node <> nil) thenbegin {if (trim (xmlattrname) = '). xmlattrname] .nodevalue : = value;} if (trim (xmlattrname) = '') thatnode.text : = valueelsebegincmpvalue : = node.attributeNodes.nodes [xmlSpecialName] .nodevalue while); nextsibling; while (node. nodename = '#comment') dobeginnode : = node.nextsibling; end; cmpvalue : = node.attributeNodes.nodes [xmlspecialName] .nodevalue; node.attributeNodes.nodes [xmlattrname] .nodevaltrname] value; end; xmldocument (StrentityEnginefile); 끝; 결과 : = true; exceptresult : = end; xmldocument.active : = false; end;