Php Xpath获取具有给定锚点的所有子级
我得到了一个XML文件,其中包含很多这样的行:Php Xpath获取具有给定锚点的所有子级,php,xml,xpath,xml-parsing,Php,Xml,Xpath,Xml Parsing,我得到了一个XML文件,其中包含很多这样的行: <class name="CustomerProfileLite" inList="true" > <enum name="Order" takeOtherValuesFromProperties="true"> <value>None</value> </enum> <property name="Guid" type="guid" />
<class name="CustomerProfileLite" inList="true" >
<enum name="Order" takeOtherValuesFromProperties="true">
<value>None</value>
</enum>
<property name="Guid" type="guid" />
<property name="CreationDate" type="datetime" isInEnum="true" />
<property name="LastUpdateDate" type="datetime" isIndexed="true" isInEnum="true" />
<property name="Revision" type="int" isIndexed="true" isInEnum="true" />
<property name="Thumbnail" type="string" convertToRelativePathInDB="true" />
<property name="UmsJob" type="string" isIndexed="true"/>
<property name="FirstName" type="string" isIndexed="true" isInEnum="true" />
<property name="LastName" type="string" isIndexed="true" isInEnum="true" />
<property name="Address" type="string" isInEnum="true" />
<property name="City" type="string" isInEnum="true" />
<property name="PhoneNumber" type="string" isIndexed="true" isInEnum="true" />
<property name="CellPhoneNumber" type="string" isIndexed="true" isInEnum="true" />
<property name="Birthdate" type="OptionalDateTime" isInEnum="true" />
<property name="HasFrames" type="bool" />
<property name="LatestEquipementHasFarVisionBoxings" type="bool" />
<property name="LatestEquipementHasFarVisionImages" type="bool" />
<property name="LatestEquipementHasSplines" type="bool" />
<property name="LatestEquipementHasIpadMeasure" type="bool" />
<sattribute name="IsModified" type="bool" />
<sattribute name="LastModificationDate" type="datetime" />
</class>
行$class->children('property')
返回一个空的simplexmlement
。另外,行$this->struct_xml->xpath(“enum”)
返回一个空值,而它应该给我一个enum列表
您能帮我修复它吗?SimpleXML::children的参数用于获取具有特定命名空间前缀的child。您必须直接指向节点。例如:
$class->property->children()
有关名称空间和前缀的更多信息,请参阅SimpleXML::children的参数用于获取具有特定名称空间前缀的childs。您必须直接指向节点。例如:
$class->property->children()
有关名称空间和前缀的更多信息,请参阅
行$class->children('property')
返回一个空的simplexmlement
虽然引用当前元素的子元素,但它的第一个参数是指示名称空间,而不是标记名
所以$class->children('property')
意味着检查命名空间属性中$class
中的子元素
您可以使用$class->property
检索该类中所有
标记的“列表”
行$this->struct_xml->xpath(“enum”)
返回一个空值,而它应该给我一个enum列表
与HTML DOM中的document.getElementByTagName
不同。您必须至少指定标记是某个对象的子对象。惰性形式应该是xpath(“//enum”)
:
$dom=simplexml_load_file("xml");
foreach($dom->xpath("//class") as $class)
{
echo (string)$class["name"];
echo "\n";
foreach($class->property as $property)
{
echo (string)$property["name"];
echo "\t";
echo (string)$property["type"];
echo "\n";
}
}
foreach($dom->xpath("//enum") as $enum)
{
echo (string)$enum["name"];
echo "\t";
echo (string)$enum->value;
}
产出:
CustomerProfileLite
Guid
创建日期时间
LastUpdateDateTime
修订版int
...
无订单
行$class->children('property')
返回一个空的simplexmlement
虽然引用当前元素的子元素,但它的第一个参数是指示名称空间,而不是标记名
所以$class->children('property')
意味着检查命名空间属性中$class
中的子元素
您可以使用$class->property
检索该类中所有
标记的“列表”
行$this->struct_xml->xpath(“enum”)
返回一个空值,而它应该给我一个enum列表
与HTML DOM中的document.getElementByTagName
不同。您必须至少指定标记是某个对象的子对象。惰性形式应该是xpath(“//enum”)
:
$dom=simplexml_load_file("xml");
foreach($dom->xpath("//class") as $class)
{
echo (string)$class["name"];
echo "\n";
foreach($class->property as $property)
{
echo (string)$property["name"];
echo "\t";
echo (string)$property["type"];
echo "\n";
}
}
foreach($dom->xpath("//enum") as $enum)
{
echo (string)$enum["name"];
echo "\t";
echo (string)$enum->value;
}
产出:
CustomerProfileLite
Guid
创建日期时间
LastUpdateDateTime
修订版int
...
无订单
请尝试使用此代码打印数据,并在该循环中添加条件
$this_struct_xml = simplexml_load_file("archi.xml"); //$this->struct_xml = $struct_xml;
foreach($this_struct_xml->property as $property) {
echo "name:".$property->attributes()->name." / ";
echo "type:".$property->attributes()->type." / ";
echo "isInEnum:".$property->attributes()->isInEnum." / ";
echo "isIndexed:".$property->attributes()->isIndexed."<br/>";
}
echo "-------------------------------<br/>";
foreach($this_struct_xml->enum as $enum) {
echo "name:".$enum->attributes()->name." / ";
echo "type:".$enum->attributes()->takeOtherValuesFromProperties." / ";
echo "Value:".$enum->value."<br/>";
}
$this_struct_xml=simplexml_load_文件(“archi.xml”)//$这->结构xml=$struct\uXML;
foreach($this_struct_xml->property as$property){
echo“name:”.$property->attributes()->name./”;
echo“type:”.$property->attributes()->type./”;
回显“isInEnum:”.$property->attributes()->isInEnum./”;
echo“isIndexed:”.$property->attributes()->isIndexed。“
”;
}
回声“------------------------------------
”;
foreach($this_struct_xml->enum as$enum){
echo“name:”.$enum->attributes()->name./”;
echo“type:”.$enum->attributes()->takeOtherValuesFromProperties./”;
回显“值:”.$enum->Value.
;
}
请尝试使用此代码打印数据,并在该循环中添加条件
$this_struct_xml = simplexml_load_file("archi.xml"); //$this->struct_xml = $struct_xml;
foreach($this_struct_xml->property as $property) {
echo "name:".$property->attributes()->name." / ";
echo "type:".$property->attributes()->type." / ";
echo "isInEnum:".$property->attributes()->isInEnum." / ";
echo "isIndexed:".$property->attributes()->isIndexed."<br/>";
}
echo "-------------------------------<br/>";
foreach($this_struct_xml->enum as $enum) {
echo "name:".$enum->attributes()->name." / ";
echo "type:".$enum->attributes()->takeOtherValuesFromProperties." / ";
echo "Value:".$enum->value."<br/>";
}
$this_struct_xml=simplexml_load_文件(“archi.xml”)//$这->结构xml=$struct\uXML;
foreach($this_struct_xml->property as$property){
echo“name:”.$property->attributes()->name./”;
echo“type:”.$property->attributes()->type./”;
回显“isInEnum:”.$property->attributes()->isInEnum./”;
echo“isIndexed:”.$property->attributes()->isIndexed。“
”;
}
回声“------------------------------------
”;
foreach($this_struct_xml->enum as$enum){
echo“name:”.$enum->attributes()->name./”;
echo“type:”.$enum->attributes()->takeOtherValuesFromProperties./”;
回显“值:”.$enum->Value.
;
}
$class->children('property')
应该是$class->property
xpath(“enum”)
应该是xpath(//enum”)
$class->children('property')
应该是$class->property
xpath(“enum”)
应该是xpath(“//enum”)