简体   繁体   English

iPhone SDK XML解析属性VS嵌套元素

[英]iPhone SDK XML Parsing Attribute VS Nested elements

I'm experiencing some performance problem using XML parser with iphone sdk. 我在将XML解析器与iphone sdk结合使用时遇到一些性能问题。 I tried to use LibXML parser directly and the NSXMLParser, but i had the same results. 我尝试直接使用LibXML解析器和NSXMLParser,但结果却相同。 Now I'm looking for something that can improve parsing performance. 现在,我正在寻找可以提高解析性能的东西。 Moving attributes to a nested elements can make it faster? 将属性移动到嵌套元素可以使其更快?

I got an XML source file like this: 我得到了这样的XML源文件:

<Events>
 <Event Name="Test1" Description="Desc" Cat="Cate" Date="20/01/2010" ImageURL="" />

</Events>

Do I have to create a nested tree ? 是否必须创建嵌套树? Such as the folliwing ? 如愚蠢? Does it improve the performance? 它会提高性能吗?

<Events>
 <Event>
  <Name>Test1</Name>
  <Description>A description</Description>
  <Date>20/01/2010</Date>
  <ImageURL>http://remoteurl.../foo.png</ImageURL>
 </Event>
</Events>

Thx a lot, Paolo 非常感谢Paolo

I am not entirely sure what you mean. 我不确定您的意思。 I may not be understanding you correctly but here is what I did in my program. 我可能无法正确理解您,但是这是我在程序中所做的。

Assume you have a file like this: 假设您有一个像这样的文件:

<document>

 <person>
  <name>bob</name>
  <phone>555-5555</phone>
   <vehicle>
    <color>blue</color>
    <type>truck</truck>
   </vehicle>
 </person>

 <person>
  <name>jan</name>
  <phone>444-5555</phone>
   <vehicle>
    <color>red</color>
    <type>car</truck>
   </vehicle>
 </person>

</document>

When you hit a person tag, lets assume your going to create a person object. 按下人员标签时,假设您要创建一个人员对象。 So you create that person object and you let it parse the xml inside of the person tags. 因此,您创建了该人员对象,并让它解析人员标签内的xml。 You do this by making the person object the new delegate of the object. 您可以通过使person对象成为该对象的新委托来实现。 When the person object discovers the closing person tag it can reset the delegate. 当人员对象发现关闭人员标签时,它可以重置委托。 I also went ahead and re-called the same method on the original delegate. 我还继续对原始委托调用相同的方法。 I got this idea from here . 我从这里得到了这个主意。 You can also follow and do the same thing for the vehicle tag. 您还可以关注车辆标签并对其执行相同的操作。 I am sure you can understand what I am driving at now. 我相信您可以理解我目前的驾驶情况。

The reason why I think this improves efficiency is that rather than creating a stack of elements and also stepping back in the stack (like when you see a person checking to see if your in a document be fore that). 我之所以认为这样可以提高效率,是因为与其创建一个元素堆栈,还不退一步(例如,当您看到有人检查文档中的内容是否在此之前)。 I suppose that you could just assume when you see a person tag that your in a person but if you want to be on the safe side checking is important. 我想您可以假设当您看到一个人标签时,您就可以在一个人中,但是如果您想安全起见,则检查很重要。 Also this way is more modular. 同样,这种方式更加模块化。 I think because of the reduction in string compares performance would improve. 我认为由于字符串比较减少,性能会提高。 Its also more elegant to code. 它的代码也更优雅。

If this is not the kinda answer your looking for could you provide a little detail. 如果不是这样,请您提供一些详细信息。 Thanks and happy coding. 谢谢,祝您编程愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM