Cómo implementar xml en Delphi
Leer nodo XML (la versión optimizada se puede usar directamente mediante COPY)
{-------------------- - -------------------------------------------------- --------
Diversión/Pro: GetXMLNodeSpecialValue
@Fecha: 2004.12.11
@Param: xmlFile archivo xml
@Param: nodo xmlnodepath
@Param: nombre del atributo xmlattrname en el nodo. Este parámetro se puede ignorar si el valor del nodo se toma directamente.
@Param: XMLSpecialName El nombre del atributo en el nodo a buscar
@Param: XMLSpecialValue El valor correspondiente a un atributo en el nodo a buscar
@Param: dep El delimitador de los parámetros del nodo, el valor predeterminado es.
@Return: el valor de un determinado atributo
---------- -------- ------------------------------------------ -------- ----------}
función GetXMLNodeSpecialValue(strEntityEngineFile:String; XMLNodePath:String;
const XMLAttrName:String=' '; const XMLSpecialName:String=' '; const XMLSpecialValue:String=''; const dep:Char ='.'):String;
var
xmlDocument :IXMLDocument;
nodo: IXMLNode;
xmlnodeList:TStrings;
i:Entero;
urlcount:Entero;
comenzar
//ruta del nodo xml
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList. DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//objeto xml
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile( strEntityEngineFile);
xmlDocument.Active:=true;
pruebe
nodo:= xmlDocument.DocumentElement;
if(node. NodeName = xmlnodeList[0]) luego comenzar
//Escanear nodos
para i:= 1 a urlcount-1 comenzar
si(nodo<> nil) entonces
comenzar
nodo := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList);
fin
else Break;
p>fin;
si(nodo=nil)entonces comienza
resultado:='';
end else comenzar
//Determina si se obtiene el atributo o el contenido del nodo
if(Trim(xmlattrname)='') luego
resultado :=node.Text
else
begin
resultado := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue //No quiero; declara una variable temporal aquí, así que usa resu
En comparación, puede haber peligros ocultos.
mientras ((resultado <> XMLSpecialValue))
comenzar
nodo := nodo.NextSibling;
mientras (nodo. NodeName = '#comment') hacer
comenzar
nodo:= nodo.NextSibling;
finalizar;
resultado:= nodo .AttributeNodes.Nodes[XMLSpecialName].NodeValue;
end;
resultado:=node.AttributeNodes.Nodes[XMLAttrName].NodeValue;
end;
fin;
fin si no comenzar
resultado:='';
fin;
excepto p>
p>
resultado:='error';
end;
xmlDocument.Active:=false;
end;
Función de escritura (la versión optimizada se puede usar directamente mediante COPIA)
{----------------------- --- ----------------------------------------------- --- --
Diversión/Pro: SetXMLNodeSpecialValue
@Fecha: 2004.12.11
@Param: xmlFile archivo xml
@Param: nodo xmlnodepath
@Param: nombre del atributo xmlattrname en el nodo, este parámetro se puede ignorar si el valor del nodo se toma directamente.
@Param: XMLSpecialName El nombre del atributo en el nodo a buscar
@Param: XMLSpecialValue El valor correspondiente a un atributo en el nodo a buscar
@Param: dep El delimitador de los parámetros del nodo, el valor predeterminado es.
@Return: si la operación fue exitosa o no
--------- ---------- ---------------------------------------- ---------- ----------}
función SetXMLNodeSpecialValue(strEntityEngineFile:String; xmlNodePath:String;
const xmlattrname:String =''; valor const:String=''; const XMLSpecialName:String=''; const XMLSpecialValue:String=''; const dep:Char ='.):boolean;
var p>
xmlDocument :IXMLDocument;
nodo :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
CMPValue :String;
begin
//ruta del nodo xml
xmlnodeList:=TStringList.Create; p>
xmlnodeList.Delimiter:= dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml objeto
xmlDocument: =TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
intenta
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) y luego comienza
//Escanear nodo
para i := 1 a urlcount-1 comience
si(nodo<>nil) entonces
nodo := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList)
else Break;
end;
if(nodo <> nil)entonces comienza
{if(Trim(xmlattrname) )='') entonces
node.Text:=value
else
node.AttributeNodes.Nodes[xmlattrname].NodeValue:=value; p>
}
if (Trim(XMLAttrName)='') entonces
node.Text := v
alue
else
comenzar
CMPValue := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
mientras (CMPValue <> XMLSpecialValue) hacer
comenzar
nodo := node.NextSibling;
mientras (node.NodeName = '#comment') hacer
comenzar
nodo:= node.NextSibling;
fin;
CMPValue := nodo.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
end;
node.AttributeNodes.Nodes[XMLAttrName].NodeValue:=valor;
end;
xmlDocument.SaveToFile(strEntityEngineFile);
fin;
fin;
resultado:=true;
excepto
resultado:=false;
end;
xmlDocument.Active:=false;
end;
Muchos usuarios en Internet informaron que la función getnodefromIXMLNodeList no estaba declarado. Ahora lo hará. La función está publicada. Como complemento a la publicación anterior
función getnodefromIXMLNodeList(childnodes:IXMLNodeList;nodename:String):IXMLNode;
var
i: Integer;
comenzar
para i :=1 a childnodes.Count comienza
if(childnodes.Get(i-1).NodeName = nodename) luego comienza
resultado:= childnodes[i-1];
salir;
fin;
fin;
fin;