开发者

Appending Nodes from a DOM Object to another DOM

Here is what i have as best i can figure what needs to be done. But im just at a loss as to how to get it to work.

Both $xml and $xmlfile are DOM objects/XML. $xml is the source of the data i want to append into $mxlfile.

$xml is sent in to the function from another function as a dom object.

This is how the xml comes to me.

<?xml version="1.0" encoding="utf-8"?>
<!-- Automatically generated data from EVE-Central.com -->
<!-- This is the new API :-) -->
<evec_api version="2.0" method="marketstat_xml">
  <marketstat>
    <type id="18">
      <all>
        <volume>1934078988.00</volume>
        <avg>98.31</avg>
        <max>26721.00</max>
        <min>0.53</min>
        <stddev>1339.78</stddev>
        <median>26.31</median>
        <percentile>0.00</percentile>
      </all>
     开发者_JAVA百科 <buy>
        <volume>1894081100.00</volume>
        <avg>20.77</avg>
        <max>31.31</max>
        <min>0.53</min>
        <stddev>7.25</stddev>
        <median>26.00</median>
        <percentile>28.17</percentile>
      </buy>
      <sell>
        <volume>39997888.00</volume>
        <avg>34.32</avg>
        <max>26721.00</max>
        <min>4.01</min>
        <stddev>2339.28</stddev>
        <median>29.80</median>
        <percentile>26.76</percentile>
      </sell>
    </type>
    <type id="ectera">
    </type>
  </marketstat>
</evec_api>

And here the part of my function i just cant get working.

$xml->preserveWhiteSpace = FALSE;
$xml->formatOutput = TRUE;
if(is_file($file)){
$xmlfile = new DOMDocument;
$xmlfile->load($file);
$dst = $xmlfile->getElementsByTagName('marketStat');
foreach($xml->getElementsByTagName('type') as $child){
 $dst->appendChild($xmlfile->importNode($child));}
$xmlfile->save($file);
}

I know there's something wrong, but im just teaching myself xml and dom, so help would be much appreciated.


<?php

if(is_file($file))
{
    $xmlfile = new DOMDocument;

    $xml->preserveWhiteSpace = FALSE;
    $xml->formatOutput = TRUE;

    $xmlfile->load($file);

    $dst = $xmlfile->getElementsByTagName('marketStat');
    $parentLength  = $dst->length;

    for($i=0; $i< $parentLength ; $i++)
    {
        foreach($xmlfile->getElementsByTagName('type') as $child)
        {
            $dst->item($i)->appendChild($child);
        }
    }
    $xmlfile->save($file);
}

Your code has two mistakes

  1. You can set options for DOM parser after you create the document.

  2. You can not use appendChild on list of DOM object. You have to add them on indivisual DOM element one by one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜