Aquí hay algunos códigos para recorrer los atributos y construir JSON. Si es compatible, uno o muchos clientes.
Si eres XML es el siguiente (o un solo cliente)
<xml>
<customer editable="true" maxChars="9" valueType="numeric">69236</customer>
<customer editable="true" maxChars="9" valueType="numeric">12345</customer>
<customer editable="true" maxChars="9" valueType="numeric">67890</customer>
</xml>
Iterar a través de él de esta manera.
try {
$xml = simplexml_load_file("customer.xml");
// Find the customer
$result = $xml->xpath('/xml/customer');
$bFirstElement = true;
echo "var customers = {\r\n";
while(list(, $node) = each($result)) {
if($bFirstElement) {
echo "'". $node."':{\r\n";
$bFirstElement = false;
} else {
echo ",\r\n'". $node."':{\r\n";
}
$bFirstAtt = true;
foreach($node->attributes() as $a => $b) {
if($bFirstAtt) {
echo "\t".$a.":'".$b."'";
$bFirstAtt = false;
} else {
echo ",\r\n\t".$a.":'".$b."'";
}
}
echo "}";
}
echo "\r\n};\r\n";
} catch(Exception $e) {
echo "Exception on line ".$e->getLine()." of file ".$e->getFile()." : ".$e->getMessage()."<br/>";
}
para producir una estructura JSON como esto
var customers = {
'69236':{
editable:'true',
maxChars:'9',
valueType:'numeric'},
'12345':{
editable:'true',
maxChars:'9',
valueType:'numeric'},
'67890':{
editable:'true',
maxChars:'9',
valueType:'numeric'}
};
Por último, en el script, acceder al atributo como esto
WScript.Echo(customers["12345"].editable);
Buena suerte
Esta falla si hay por ejemplo un bloque CDATA en el elemento, así: –
Para mí, la solución fue llamar explícitamente a '-> niños()' al buclear el objeto xml. Si no lo hice, los atributos desaparecieron. –