这个DOMDocument XML的SimpleXML版本是什么?

时间:2022-03-25 07:51:11

I understand that SimpleXML is far more efficient than DOMDocument. Any advice on how I would reform the below into a SimpleXML version?

我知道SimpleXML比DOMDocument更有效。关于如何将下面的内容改为SimpleXML版本的任何建议?

    <?php
  $doc = new DOMDocument();
  $doc->load( 'feedpage.xml' );


  $Main = $doc->getElementsByTagName( "varOne" );
  foreach( $Main as $varOne )
  {
  $VarTwo = $varOne->getElementsByTagName( "VarTwo" );
  $VarTwo = $VarTwo->item(0)->nodeValue;

  $VarThree = $varOne->getElementsByTagName( "VarThree" );
  $VarThree = $VarThree->item(0)->nodeValue;

  $VarFour = $varOne->getElementsByTagName( "VarFour" );
  $VarFour = $VarFour->item(0)->nodeValue;

  $VarFive = $varOne->getElementsByTagName( "VarFive" );
  $VarFive = $VarFive->item(0)->nodeValue;

  echo "$VarTwo - $VarThree - $VarTFour - ETC\n";
  echo "<img src=\"$VarFive\" />";
  echo "<a href=\"$VarFour\" target=\"_blank\">Link</a>";
  }
  ?>

1 个解决方案

#1


1  

Start with something like this:

从这样的事情开始:

$doc = simplexml_load_file("feedpage.xml");

Then (since I don't know what your XML file looks like), try:

然后(因为我不知道您的XML文件是什么样的),请尝试:

echo "<pre>".print_r($doc,true)."</pre>";

to see exactly how the resulting object is laid out. From there, you should be able to pick out the pieces you need to build what you want.

准确了解生成的对象是如何布局的。从那里,你应该能够挑选出你想要的东西。

Edit:

编辑:

If your output is:

如果您的输出是:

SimpleXMLElement Object ( 
    [varOne] => SimpleXMLElement Object ( 
        [varOne] => Title 
        [varTwo] => A description 
        [VarThree] => A Link 
        [VarFour] => An Image
    )
)

You could do this to access the properties of each one:

您可以这样做来访问每个属性:

foreach($doc as $row) {
    $title = $row->varOne;
    $description = $row->varTwo;
    // etc.
}

So with the foreach loop, you can go through each main item and access each item's properties.

因此,使用foreach循环,您可以浏览每个主项并访问每个项的属性。

And if you want to put code in comments, you can use the backtick (`) to surround your text, but it doesn't work well for code blocks (like the one you wanted to post). Best for variables or other short bits.

如果你想把代码放在注释中,你可以使用反引号(`)来包围你的文本,但它不适用于代码块(比如你想发布的代码块)。最适合变量或其他短位。

FINAL EDIT:

最终编辑:

Let's take this example object:

我们来看看这个示例对象:

SimpleXMLElement Object ( 
    [varOne] => SimpleXMLElement Object ( 
        [varOne] => "Title"
        [varTwo] => "Description"
        [varThree] => "Link"
        [varFour] => "Image"
        [varFive] => SimpleXMLElement Object (
            [varA] => "something"
            [varB] => SimpleXMLElement Object (
                [varX] => "a string"
            )
            [varC] => "another thing"
        )
    )
)

Let's say the whole thing is contained in a variable $obj. Now let's say we wanted what's in varX. We'd access it like this:

假设整个事物包含在变量$ obj中。现在让我们说我们想要varX中的内容。我们这样访问它:

echo $obj->varOne->varFive->varB->varX;

Beyond this, I don't know what else to tell you. You need to closely examine the objects you have and determine how they are structured. Extrapolate what you've learned here and apply it to your situation.

除此之外,我不知道还能告诉你什么。您需要仔细检查您拥有的对象并确定它们的结构。推断您在这里学到的知识,并将其应用到您的情况中。

#1


1  

Start with something like this:

从这样的事情开始:

$doc = simplexml_load_file("feedpage.xml");

Then (since I don't know what your XML file looks like), try:

然后(因为我不知道您的XML文件是什么样的),请尝试:

echo "<pre>".print_r($doc,true)."</pre>";

to see exactly how the resulting object is laid out. From there, you should be able to pick out the pieces you need to build what you want.

准确了解生成的对象是如何布局的。从那里,你应该能够挑选出你想要的东西。

Edit:

编辑:

If your output is:

如果您的输出是:

SimpleXMLElement Object ( 
    [varOne] => SimpleXMLElement Object ( 
        [varOne] => Title 
        [varTwo] => A description 
        [VarThree] => A Link 
        [VarFour] => An Image
    )
)

You could do this to access the properties of each one:

您可以这样做来访问每个属性:

foreach($doc as $row) {
    $title = $row->varOne;
    $description = $row->varTwo;
    // etc.
}

So with the foreach loop, you can go through each main item and access each item's properties.

因此,使用foreach循环,您可以浏览每个主项并访问每个项的属性。

And if you want to put code in comments, you can use the backtick (`) to surround your text, but it doesn't work well for code blocks (like the one you wanted to post). Best for variables or other short bits.

如果你想把代码放在注释中,你可以使用反引号(`)来包围你的文本,但它不适用于代码块(比如你想发布的代码块)。最适合变量或其他短位。

FINAL EDIT:

最终编辑:

Let's take this example object:

我们来看看这个示例对象:

SimpleXMLElement Object ( 
    [varOne] => SimpleXMLElement Object ( 
        [varOne] => "Title"
        [varTwo] => "Description"
        [varThree] => "Link"
        [varFour] => "Image"
        [varFive] => SimpleXMLElement Object (
            [varA] => "something"
            [varB] => SimpleXMLElement Object (
                [varX] => "a string"
            )
            [varC] => "another thing"
        )
    )
)

Let's say the whole thing is contained in a variable $obj. Now let's say we wanted what's in varX. We'd access it like this:

假设整个事物包含在变量$ obj中。现在让我们说我们想要varX中的内容。我们这样访问它:

echo $obj->varOne->varFive->varB->varX;

Beyond this, I don't know what else to tell you. You need to closely examine the objects you have and determine how they are structured. Extrapolate what you've learned here and apply it to your situation.

除此之外,我不知道还能告诉你什么。您需要仔细检查您拥有的对象并确定它们的结构。推断您在这里学到的知识,并将其应用到您的情况中。