0

嗨,我期待创建一个表单,让人们将新条目上传到 .xml 文件中。表单不是问题,对我来说问题是如何将新条目(动态和静态)添加到现有的 .xml 文件中。

我的 XML 文件如下所示:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<item><provider>Fabian</provider>
<provider_itemid>1</provider_itemid>
<name>Robe der Regalia</name>
<origin>Art: Drop</origin>
<description>Mindest-Stufe: Keine</description>
<extension>CLASSIC</extension>
<position>CHEST</position>
<online_url>www.google.me</online_url>
<lastupdate>28.03.2008 09:58:20</lastupdate>
<realm>Hibernia</realm>
<level>30</level>
<quality>95</quality>
<bonus>20</bonus>
<armor af="30">CLOTH</armor>
<effect id="HITPOINTS">27</effect>
<effect id="ENCHANTMENTS">2</effect>
<effect id="MENTALISM">2</effect>
<effect id="VOID">2</effect>
</item>
</daoc_items>

如果有人有样品就好了!:) 谢谢

更新解决方案

<?php
  $daoc_items = array();
  $daoc_items [] = array(
  'name' => 'Skyros-Hammer aus Arkanit',
  'extensions' => 'test',
  'description' => 'test',
  'realm' => 'Midgard',
  'level' => 'Midgard',
  'quality' => "99",
  'bonus' => "99",
  'position' => "99",
  'weapon' => "99",
  'effect' => "99",
  'origin' => "99",
  );
  $daoc_items [] = array(
  'name' => 'Skyros-Hammer aus Arkanit',
  'extensions' => 'test',
  'description' => 'test',
  'realm' => 'Midgard',
  'level' => 'Midgard',
  'quality' => "99",
  'bonus' => "99",
  'position' => "99",
  'weapon' => "99",
  'effect' => "99",
  'origin' => "99",
  );

  $doc = new DOMDocument();
  $doc->formatOutput = true;

  $r = $doc->createElement( "daoc_items" );
  $doc->appendChild( $r );

  foreach( $daoc_items as $item )
  {
  $b = $doc->createElement( "item" );

  $name = $doc->createElement( "name" );
  $name->appendChild(
  $doc->createTextNode( $item['name'] )
  );
  $b->appendChild( $name );

  $extensions = $doc->createElement( "extensions" );
  $extensions->appendChild(
  $doc->createTextNode( $item['extensions'] )
  );
  $b->appendChild( $extensions );

  $description = $doc->createElement( "description" );
  $description->appendChild(
  $doc->createTextNode( $item['description'] )
  );
  $b->appendChild( $description );

  $realm = $doc->createElement( "realm" );
  $realm->appendChild(
  $doc->createTextNode( $item['realm'] )
  );
  $b->appendChild( $realm );

  $level = $doc->createElement( "level" );
  $level->appendChild(
  $doc->createTextNode( $item['level'] )
  );
  $b->appendChild( $level );

  $quality= $doc->createElement( "quality" );
  $quality->appendChild(
  $doc->createTextNode( $item['quality'] )
  );
  $b->appendChild( $quality);

  $bonus= $doc->createElement( "bonus" );
  $bonus->appendChild(
  $doc->createTextNode( $item['bonus'] )
  );
  $b->appendChild( $bonus);

  $position= $doc->createElement( "position" );
  $position->appendChild(
  $doc->createTextNode( $item['position'] )
  );
  $b->appendChild( $position);

  $weapon= $doc->createElement( "weapon" );
  $weapon->appendChild(
  $doc->createTextNode( $item['weapon'] )
  );
  $b->appendChild( $weapon);

  $effect= $doc->createElement( "effect" );
  $effect->appendChild(
  $doc->createTextNode( $item['effect'] )
  );
  $b->appendChild( $effect);

  $origin= $doc->createElement( "origin" );
  $origin->appendChild(
  $doc->createTextNode( $item['origin'] )
  );
  $b->appendChild( $origin);

  $r->appendChild( $b );
  }

  echo $doc->saveXML();
  $doc->save("item.xml")
  ?> 
4

1 回答 1

0

您可以使用 xml 解析器/类中的一种构建,例如 simplexml:

$filename = 'my_xml_file.xml';
$items = simplexml_load_file($filename);

$items->item[0]->addChild('tagname', 'value');
$items->asXML($filename);

当然,您必须找到一种方法来确定要将孩子添加到哪个项目(检查该行,说明$items->item[0]现在只获得第一个项目)。

阅读http://nl.php.net/manual/en/book.simplexml.php上的文档,并查看其他 xml 操作类,特别是 DOM,然后选择您最熟悉的类

于 2012-04-19T14:16:33.760 回答