元素值的Php xml解析

元素值的Php xml解析,php,Php,我从jsp调用返回了以下xml <assumption name="test" id="34" description="vector description" is_shared="no" vector_type="Prepay"> <userVector> <vectorType>"Prepay"</vectorType> <ppydefTypeCode>"CDR"</ppydefTypeCode> <ppydef

我从jsp调用返回了以下xml

<assumption name="test" id="34" description="vector description" is_shared="no" vector_type="Prepay">
<userVector>
<vectorType>"Prepay"</vectorType>
<ppydefTypeCode>"CDR"</ppydefTypeCode>
<ppydefVector>
<vectorName>"test"</vectorName>
<vectorSeasoning>No</vectorSeasoning>
<vectorPeriod>"6 12"</vectorPeriod>
<vectorData>"7 4"</vectorData>
</ppydefVector>
</userVector>
</assumption>
getElementsByTagName'vectorType'将返回一个DOMNodeList,您还需要遍历该DOMNodeList。通过访问每个元素的nodeValue字段,可以将其值保存到变量中

试试这个:

$sx = new SimpleXMLElement($XMLData);
echo $sx->xpath('/assumption/userVector/vectorType')[0];
echo $sx->xpath('/assumption/userVector/ppydefVector/vectorName')[0];
产出:

"Prepay"
"test"

不用担心,希望能有所帮助:如何检查vectorType元素是否存在技术上我们必须假设可能存在多个vectorType,因此您需要检查$vectorTypes的大小是否大于0。
$sx = new SimpleXMLElement($XMLData);
echo $sx->xpath('/assumption/userVector/vectorType')[0];
echo $sx->xpath('/assumption/userVector/ppydefVector/vectorName')[0];
"Prepay"
"test"