将xml传递给php函数
我正在考虑将XML字符串传递给函数,然后返回父节点及其值 举个例子:将xml传递给php函数,php,xml,Php,Xml,我正在考虑将XML字符串传递给函数,然后返回父节点及其值 举个例子: $xml = "<Student><Name>Jee Pee</Name><Age>16</Age></Student>"; runXmltoStr($xml); function runXmltoStr($xml) { // This is where I can't figure out where to start // In my mind, I
$xml = "<Student><Name>Jee Pee</Name><Age>16</Age></Student>";
runXmltoStr($xml);
function runXmltoStr($xml)
{
// This is where I can't figure out where to start
// In my mind, I do have this output
//
// Student
// Name: Jee Pee
// Age: 16
}
$xml=“Jee Pee16”;
runXmltoStr($xml);
函数runXmltoStr($xml)
{
//这就是我不知道从哪里开始的地方
//在我看来,我有这个输出
//
//学生
//姓名:Jee Pee
//年龄:16
}
您应该使用XML解析器,例如:
使用
将字符串加载到XML元素中,然后使用SimpleXML:获取元素。也许您可以看看可以为您完成所有这类工作的
DOM不是解析XML的最快方法,但它可靠且易于在复杂的XML数据结构上实现。首先阅读PHP XML解析器:谢谢@Tim Cooper。我试图
print\r($xml\u节点)代码>但我没有看到数组键上的学生。我查找它的目的是,我希望将其包含在我的输出中。@Dee:就像我发布的一样,您可以像访问对象属性一样访问节点,如下所示:$simplexml\u node->SubnodeName
。
$xml_node = simplexml_load_string($xml);
$xml_node->Name; // Jee Pee
$xml_node->Age; // 16
simplexml_load_string($xml);