Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用CFSCRIPT获取xml属性_Xml_Coldfusion_Xml Parsing - Fatal编程技术网

使用CFSCRIPT获取xml属性

使用CFSCRIPT获取xml属性,xml,coldfusion,xml-parsing,Xml,Coldfusion,Xml Parsing,简单XML <employee> <name EmpType="Regular"> <first>Almanzo</first> <last>Wilder</last> </name> </employee> 阿尔曼佐 野性的 我正在尝试使用CFSCRIPT测试属性“EmpType”是否存在 尝试使用isDefined(“\u XMLDo

简单XML

<employee> 
    <name EmpType="Regular"> 
        <first>Almanzo</first> 
        <last>Wilder</last> 
    </name> 
</employee>

阿尔曼佐
野性的
我正在尝试使用CFSCRIPT测试属性“EmpType”是否存在

尝试使用isDefined(“\u XMLDoc.employee.name[1].xmldattributes.EmpType”); 无济于事

尝试使用structkeyexists(_XMLDoc,'employee.name[1].xmldattributes.EmpType'); 无济于事

还有其他想法吗?

试试这个

<cfscript>
   employee.name[1].xmlAttributes["EmpType"]
</cfscript>

employee.name[1].xmlAttributes[“EmpType”]

我使用CFSCRIPT解析XML

测试节点是否存在时,应使用structKeyExists();这需要两个参数,范围和变量。范围不能用引号括起来。变量必须在引号中

structKeyExists(SCOPE, "Variable");
这里有一小段可能会有帮助

// BOOTH NUMBER
BoothInfo.BoothNumber = ResponseNodes[i].BoothNumber.XmlText;
writeOutput("<h3>BoothNumber - #BoothInfo.BoothNumber# </h3>");

// CATEGORIES
if (structKeyExists(ResponseNodes[i].ProductCategories, "Category")) {
    Categories = ResponseNodes[i].ProductCategories.Category;
    CatIDList = "";
    for (j = 1; j lte arrayLen(Categories); j++) {
        CatID = ResponseNodes[i].ProductCategories.Category[j].XmlAttributes.ID;
        CatIDList = listAppend(CatIDList, CatID);
    }
BoothInfo.CatID = CatIDList;
} else {
BoothInfo.CatID = "";
}
writeOutput(BoothInfo.CatID);
//展位号
BoothInfo.BoothNumber=ResponseNodes[i].BoothNumber.XmlText;
写输出(“BoothNumber-#BoothInfo.BoothNumber#”);
//类别
if(structKeyExists(ResponseNodes[i].ProductCategories,“Category”)){
Categories=响应节点[i].ProductCategories.Category;
CatIDList=“”;
对于(j=1;j lte arrayLen(类别);j++){
CatID=ResponseNodes[i].ProductCategories.Category[j].XmlAttributes.ID;
CatIDList=listAppend(CatIDList,CatID);
}
BoothInfo.CatID=CatIDList;
}否则{
BoothInfo.CatID=“”;
}
写输出(BoothInfo.CatID);
这是我的解决方案:

myEmpType = '';
    try{
    myEmpType = _XMLDoc.employee.name[1].xmlAttributes["EmpType"]; 
} catch (Any e) {
    myEmpType = 'no category';
}

我同意这是你想要改变的:

structkeyexists(_XMLDoc,'employee.name[1].xmlAttributes.EmpType')
为此:

 structkeyexists(_XMLDoc.employee.name[1].xmlAttributes,'EmpType')

除了要检查的最后一项之外,您希望所有项都作为第一个参数。

使用
xmlsearch

为了测试这一点,我在XML中添加了更多内容

<employee> 
<name EmpType="Regular"> 
    <first>Almanzo</first> 
    <last>Wilder</last> 
</name>
<name> 
    <first>Luke</first> 
    <last>Skywalker</last> 
</name> 
</employee>

关于
xmlsearch
和。

我会使用XPath来解决这个问题:

XmlSearch(xmlObject, "//name[@EmpType]")
上面的表达式返回一个具有属性EmpType的节点数组,如果没有找到,则返回一个空数组。因此,您可以检查数组是否为空。如果您只需要一个布尔值,可以修改xpath字符串,如下所示:

XmlSearch(xmlObject, "exists(//name[@EmpType])")

此表达式使用xpath函数
exists()
,如果找到任何具有该属性的节点,则返回true;如果没有,则返回false。整个表达式返回true或false,而不是第一种情况下的数组。
XmlSearch()
的返回类型根据xpath表达式返回的内容而变化。

我尝试了这个方法,但不幸的是,它没有找到属性的存在。我不建议您全部使用它。这只是一个有很多可能性的例子。这不是我建议的吗?
XmlSearch(xmlObject, "exists(//name[@EmpType])")