小编ben*_*son的帖子

使用 PHP DOMDocument 时 nextSibling 不起作用

我尝试使用以下元素获取以下元素nextSibling,但以下代码不起作用。我有一个错误,如下所示 PHP 警告:为第 35 行 /php/dom.php 中的 foreach() 提供的参数无效

这一定是由 foreach 循环中的空值引起的。

但是如果我修改它以获取使用previousSibling它的前一个元素,它会按预期工作。

$doc = new DOMDocument();
$html = <<<HTML
<html>
<body>
<ul id="list">
<li>Foo</li>
<li>Bar</li>
</ul>
<h2 class = 'test'>heading2</h2>
<ul id="list2">
<li>list1</li>
<li>list2</li>
</ul>
</body>
</html>
HTML;

$doc ->loadHTML($html);

$DOMNodelist = $doc->getElementsByTagName('*');

foreach($DOMNodelist as $node) {
    if ($node -> hasAttribute('class')) {    
        foreach($node -> nextSibling ->childNodes as $morenodes) {
           print_r($morenodes);
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

php domdocument

3
推荐指数
1
解决办法
3929
查看次数

将元素添加到链表(C)

我想创建一个链接列表,我可以在以后添加更多元素,但我对当前代码的问题是所有以前的元素被最后添加的元素覆盖.这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node {
    char *name;
    struct node *next;
}*head;



void add( char *str ) {
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->name=str;

    if (head== NULL) {
        head=temp;
        head->next=NULL;

    } else {
        temp->next=head;
        head=temp;
    }
}

void  display(struct node *r) {
    r=head;
    if(r==NULL)
        return;

    while(r!=NULL) {
        printf("%s ",r->name);
        r=r->next;
    }

    printf("\n");
}

int  main()
{
    char *str;
    struct node *n;
    head=NULL;
    while(scanf("%s",str) == 1) {
        add(str);
        display(n);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c structure linked-list

0
推荐指数
1
解决办法
109
查看次数

将返回的指针赋给变量时出现分段错误

我有以下代码

char * find(struct node *r,char *str)
  {
      r=head;

     if(r==NULL)
     {
        return NULL;
     }

    while(r!=NULL)
   { 
       if((strcmp(str,r->name) == 0))
         {
           printf("found %s\n",r->name);
           char *p = malloc(256);
           strcpy(p,(const char *)r->name);
           return p;
         }
      r=r->next;

   }

  return NULL;

 }
Run Code Online (Sandbox Code Playgroud)

并在main:

void main () 
  {
    ......
    ......
    char *ret;
    ret = find(n,"someone");
    printf("%s\n",ret);
    .......
 }
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,我总是有分段错误.但是当我取消ret变量时.有用.

void main () 
{
   ......
   ......
   printf("%s\n",find(n,"someone"));
   .......
}
Run Code Online (Sandbox Code Playgroud)

c pointers

-1
推荐指数
1
解决办法
99
查看次数

标签 统计

c ×2

domdocument ×1

linked-list ×1

php ×1

pointers ×1

structure ×1