PHP中的类-操作XML(2)

 

name of the tag -- in the first case above, this is the tag STORY, and
below that BYLINE.  You want BYLINE_AUTHOR.  You want the first BA.  The
first one is index [0] in the second part of the two-dimensional array.

Even if there is only *one* byline author, it's still an array, and you
still have to use the [0].  Now, the two-dimensional array is storing
dynamic structures -- objects in this case.  So, we need to dereference
the object, hence the ->.  The BYLINE_AUTHOR is the tag you want, and it
is an array in that object.  The reason for the array is that if there are
more than one BYLINE_AUTHOR for the tags STORY, BYLINE, we would have a
[0] and [1] in the array.  In your case there is just the one.

*** This is very confusing, I know, but once you understand it, the power
of this method will be more apparent.  You have access to *every* bit of
information in the XML file, without having to do anything but understand
how to refer to the variables. ***

EVERY variable will look like this:
       print $xml["STORY_BYLINE"][0]->BYLINE_AUTHOR[0];

The trick is understanding how to get the variable to give you the
information.  This is an array of arrays of objects holding arrays!

Any tag that has attributes will have them stored in a special object
array named "attributes" and will be called this way:

       print $xml["STORY"][0]->attributes[0]["TIMESTAMP"];

If you aren't sure if there are attributes, you could do isset() or
is_array() for that above example.  If isset(), you could for loop and
while(list($k,$v) = each($xml...)) over it to get the values.

         array of
                 objects
                    |
                    |
$xml["STORY_BYLINE"][0]->BYLINE_AUTHOR[0];
          ^                    ^
       array of                ^
        arrays                 ^
                               ^
                            array in
                             object

In general, to get the value of this:

<STATE>
       <STATENAME></STATENAME>
       <COUNTY>
               <COUNTYNAME></COUNTYNAME>
               <CITY></CITY>
               <CITY></CITY>
       </COUNTY>
       <COUNTY>
               <COUNTYNAME></COUNTYNAME>
               <CITY></CITY>
               <CITY></CITY>
       </COUNTY>
</STATE>

You would look for what you want, say "CITY", then go UP one level, to
COUNTY (COUNTYNAME is on the same 'level'), for your first array:

$xml["STATE_COUNTY"] -- ALL tags pushed together are separated with
"_".  Otherwise tags are as they were -- spaces, dashes, CaSe, etc.

Now, you want the first COUNTY, though there are two, so we are do this:

$xml["STATE_COUNTY"][0] -- to get the second, we'd use [1] instead of
[0].  You could also do a for() loop through it, using sizeof() to figure
out how big it is.

So, we have the STATE,COUNTY we want -- the first one.  It's an
object, and we know we want the CITY.  So, we dereference the object.  The
name of the array we want is, of course, CITY:

$xml["STATE_COUNTY"][0]->CITY[0] (the first one, the second one would be
[1]).

And that's it.  Basically, find what you want, and go up a level.

You could do some complex for loops to go through them all, too:

for($i=0;$i<sizeof($xml["STATE_COUNTY"]);$i++) {

       for($j=0;$j<sizeof($xml["STATE_COUNTY"][0]->CITY);$j++) {

               print $xml["STATE_COUNTY"][$i]->CITY[$j];

       }

}

-----------

Whew.  I hope that helps, not hurts.

*/

/* used to store the parsed information */
class xml_container {

   function store($k,$v) {
       $this->{$k}[] = $v;
   }

}

/* parses the information */
class xml {  

   // initialize some variables
   var $current_tag=array();
   var $xml_parser;
   var $Version = 1.0;
   var $tagtracker = array();<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

时间: 2024-09-17 23:05:20

PHP中的类-操作XML(2)的相关文章

限制程序中某类操作的执行次数的算法设计及C代码实现

需求描述 编写程序实现限制程序中某类操作的执行次数的需求.为了便于说明,要求程序每天创建一个与上一天不同的文件目录.如果欲创建的文件目录已存在,则不用再创建.文件目录的命名格式为:FileDir_YYYYMMDD,如:FileDir_20160830. 程序流程 对于此类需求,最关键的问题是要设定一个标识来限制操作的执行次数.也就是说,当程序执行完一次操作之后,要有机制来限制它执行第二次操作. 因为本需求要求每天执行一次操作,所有我们自然想到了用日期来限制程序的执行次数.我们可以用一个全局时间变

sql server中使用T-Sql操作Xml数据

一.前言 SQLServer 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和参数.为了更好地支持 XML 模型特征(例如文档顺序和递归结构),XML 值以内部格式存储为大型二进制对象 (BLOB). 用户将一个XML数据存入数据库的时候,可以使用这个XML的字符串,SQL Server会自动的将这个字符串转化为XML类型,并存储到数据库中. 随着SQL Server 对XML字段的支持,相应的,T-S

java中四种操作xml方式的比较

xml|比较   1. 介绍 1)DOM(JAXP Crimson解析器)         DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特定信息.分析该结构通常需要加载整个文档和构造层次结构,然后才能做任何工作.由于它是基于信息层次的,因而DOM被认为是基于树或基于对象的.DOM以及广义的基于树的处理具有几个优点.首先,由于树在内存中是持久的,因此可以修改它以便应用程序能对数据和结构作出更改.它还

C#中TreeView类操作全攻略(一)

treeview|攻略 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using com.prm.client.tools;using System.Data.OracleClient;using com.prm.client.common;using com.prm.client

今天看到一个很好的类-操作xml的!贴出来给大家,不知道以前贴过没有?

xml <?/*     (c) 2000 Hans Anderson Corporation.  All Rights Reserved.     You are free to use and modify this class under the same     guidelines found in the PHP License.     -----------     bugs/me:     http://www.hansanderson.com/php/     me@hans

C#中TreeView类操作全攻略(二)

treeview|攻略 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using com.prm.client.tools;using System.Data.OracleClient;//using com.prm.client.common; namespace com.prm

winfor中在treeview操作XML节点添加删除操作问题 求助!!!

问题描述 //删除节点按钮privatevoidbutton3_Click(objectsender,EventArgse){try{TreeNodeactiveNode=treeView1.SelectedNode;if(activeNode==null){return;}#region删除当前选择的节点XmlNodexmlNode=activeNode.TagasXmlNode;XmlNodeparentNode=xmlNode.ParentNode;if(parentNode==null)

C#中TreeView类操作全攻略(三)

treeview|攻略 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using com.prm.client.tools;using System.Data.OracleClient;//using com.prm.client.common; namespace com.prm.client.forms{ ///

C#操作XML增删改查

原文:C#操作XML增删改查 XML文件是一种常用的文件格式,不管是B/S还是C/S都随处可见XML的身影.Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具.XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然XML占用的空间比二进制数据要占用更多的空间,但XML极其简单易于掌握和使用.微软也提供了一系列类库来倒帮助我们在应用程序中存储XML文件.     "在程序中访问进而操作XML文件一般有两种模型,分

JDOM操作XML文档

  解析 xml文档的接口技术有很多 ,DOM  JDOM  SAX  ..其中JDOM技术是最简单的操作,代码操作比DOM  SAX少很多 . 关于这三种技术的介绍可以去网上查询一下. SAX是基于事件响应的 (没用过) . DOM是java官方的标准,我们在操作xml的时候其实是在内存中构建了一棵文档树,对于较小的xml文档可以使用dom处理,但是对于数据量比较大的XML文档,DOM比较耗费内存. JDOM是一个开源的项目,融合了DOM和SAX技术 ,轻量级的API可以方便的操作XML文档,