php通过查询元素的值来加载/获取xml子元素

时间:2016-02-12 15:42:46

标签: php xml post xpath

数据,来自我的xml文件的值,基于发布的名称。

这是我的xml文件:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct linkedList
{
    int data;
    struct linkedList *next;
};
struct linkedList* createNode(int value)
{
    struct linkedList *node;
    node = malloc(sizeof(struct linkedList));
    node->next = malloc(sizeof(struct linkedList));
    node->next = NULL;
    node = NULL;
    return node;
}
//insert a node at the top of the linked list
struct linkedList* insertTop(struct linkedList* top,struct linkedList* node)
{
    if(top == NULL)//the element we insert is the 1st element for the linked list
    {
        node->next = NULL;
        //the first element points to NULL since it has no successors
    }
    else//there is already an element in the list
    {
        node->next = top;
    }
    return node;
}
void iterate(struct linkedList* top)
{
    while(top->next != NULL)
    {
        printf("Data = %d\n", top->data);
        top = top->next;
    }
}
int main()
{
    struct linkedList *a,*b,*c,*root;
    a = createNode(2);
    b = createNode(10);
    c = createNode(23);
    root = insertTop(NULL,a);//these 3 lines provoke a segmentation fault
    root = insertTop(root,b);
    root = insertTop(root,c);
    iterate(root);//the result I expect here is 23,10,2 
    return 0;
}

这是我的php文件:

<object>
  <name>test 1</name>
  <data>some data</data>
  <value>1</value>
</object>
<object>
  <name>test 2</name>
  <data>some data 2</data>
  <value>2</value>
</object>

这会返回一个空数组,我一直在寻找答案,但我没有找到任何帮助,所以感谢任何帮助

1 个答案:

答案 0 :(得分:1)

这里有几个问题:

  1. XML没有根元素,因此无效。
  2. xpath中的路径不完整
  3. xpath中的条件语法错误
  4. ad 1:使用根节点包含XML,例如:

    <root>
        <object>
            <name>test 1</name>
        </object>
    </root>
    

    ad 2和3:路径和条件语法:

    $xpath = $xml->xpath("/root/object[name = 'test 1']");
    

    $xpath = $xml->xpath("//object[name = 'test 1']");
    

    在行动中看到它:https://eval.in/517818