2010-12-15 5 views
0

Je veux extraire tous les nœuds (parents/enfants) avec le nom « personne » à l'aide coldfusion et rendre la valeur de l'attribut « image1 » comme NULLTrouver tous les nœuds XML à l'aide coldfusion

<person image1="img1.jpg" image2="img2.jpg"> 
    <person image1="img3.jpg" image2="img4.jpg" /> 
    <person image1="img5.jpg" image2="img6.jpg" /> 
</person> 

Répondre

3

Voici une solution à votre (modifiée) question en utilisant la récursivité:

<cfset s = '<person image1="img1.jpg" image2="img2.jpg"> 
    <person image1="img3.jpg" image2="img4.jpg" /> 
    <person image1="img5.jpg" image2="img6.jpg" /> 
</person>' /> 

<cfset doc = xmlParse(s) /> 

<cfdump var="#doc#" label="before" /> 

<cfset myFunction(doc.xmlRoot) /> 

<cfdump var="#doc#" label="after" /> 

<!--- Function ---> 
<cffunction name="myFunction" output="true"> 
    <cfargument name="doc" type="xml" /> 

    <cfif ARGUMENTS.doc.xmlName eq "person"> 

     <cfloop collection="#ARGUMENTS.doc.xmlAttributes#" item="LOCAL.k"> 
      <p>#ARGUMENTS.doc.xmlAttributes[LOCAL.k]# set to null</p> 
      <cfset ARGUMENTS.doc.xmlAttributes[LOCAL.k] = "null" /> 
     </cfloop> 

    </cfif> 

    <!--- Recursively call for children ---> 
    <cfloop array="#ARGUMENTS.doc.xmlChildren#" index="LOCAL.childElem"> 
     <cfset myFunction(LOCAL.childElem) /> 
    </cfloop> 

</cffunction> 

Hope that helps!

+0

+1 si seulement parce que je suis trop paresseux maintenant pour changer ma réponse. – orangepips

1
<cfscript> 
doc = xmlparse(/*xml from above*/); 
selectedElements = xmlsearch(doc, "//language"); 

for (i=1; i LTE ArrayLen(selectedElements); i = i + 1) { 
    // do what you want with the nodes here 
} 
</cfscript> 
+0

Salut merci pour la réponse, j'ai modifié ma question ... pls vérifier la question – kayteen

Questions connexes