开发者

SimpleXML: how to replace node name?

开发者 https://www.devze.com 2023-01-25 02:58 出处:网络
I\'m trying to output xml file as html. It seems like simpleXML processing is faster than XSL on my files (files are 2-3mb each). So I ended with simpleXML. Everything seems fine, files loaded ok with

I'm trying to output xml file as html. It seems like simpleXML processing is faster than XSL on my files (files are 2-3mb each). So I ended with simpleXML. Everything seems fine, files loaded ok with $data = simplexml_load_string($file) but now I need to replace node names wi开发者_开发知识库th html tag names and build a new document with $data->asXML(). Is there any way to do this?

What I have now:

    <body>
 <section>
  <title>
   <p>sometext</p>
  </title>
  <epigraph>
    <p>sometext</p>
  </epigraph>
  <stanza>
    <v>sometext</v>
    <v>sometext</v>
    <v>sometext</v>
    <v>sometext</v>
  </stanza>
  <stanza>
    <v>sometext</v>
    <v>sometext</v>
    <v>sometext</v>
    <v>sometext</v>
  </stanza>
   <section>
    <title>
     <p>sometext</p>
    </title>
    <epigraph>
      <p>sometext</p>
    </epigraph>
    <stanza>
      <v>sometext</v>
      <v>sometext</v>
      <v>sometext</v>
      <v>sometext</v>
    </stanza>
    <stanza>
      <v>sometext</v>
      <v>sometext</v>
      <v>sometext</v>
      <v>sometext</v>
    </stanza>
   </section>
 </section>
</body>

And I'd like to have this:

   <body>
 <div class="section">
  <h2>
   <span>sometext</span>
  </h2>
  <div class="epigraph">
    <p>sometext</p>
  </div>
  <ul>
    <li>sometext</li>
    <li>sometext</li>
    <li>sometext</li>
    <li>sometext</li>
  </ul>
  <ul>
    <li>sometext</li>
    <li>sometext</li>
    <li>sometext</li>
    <li>sometext</li>
  </ul>
   <div class="section">
    <h2>
     <span>sometext</span>
    </h2>
    <div class="epigraph">
      <p>sometext</p>
    </div>
    <ul>
      <li>sometext</li>
      <li>sometext</li>
      <li>sometext</li>
      <li>sometext</li>
    </ul>
    <ul>
      <li>sometext</li>
      <li>sometext</li>
      <li>sometext</li>
      <li>sometext</li>
    </ul>
   </div>
 </div>
</body>

thanks in advance


AFAIK, you can't do that directly. You can either

  • Parse it and loop over using foreach and such,
  • Or, if the xml is guaranteed to be that simple and consistent, you can simply do something like
    strtr($xml, array('stanza>' => 'ul>', 'v>' => 'li>', ...));
0

精彩评论

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