开发者

display a xml document without the tags?

开发者 https://www.devze.com 2023-01-30 15:38 出处:网络
i have file called data_out.xml which contains this xml code: <?xml version=\"1.0\" ?> - <allproperty>

i have file called data_out.xml which contains this xml code:

<?xml version="1.0" ?> 
    - <allproperty>
    - <aproperty>
      <postcode>ha15rs</postcode> 
      <price>250</price> 
      <imagefilename>home2.gif</imagefilename> 
      <visits>2</visits> 
      </aproperty>
    - <aproperty>
      <postcode>ha36gs</postcode> 
      <price>150</price> 
      <imagefilename>home3.gif</imagefilename> 
      <visits>1</visits> 
      </aproperty>
    - <aproperty>
      <postcode>ha27se</postcode> 
      <price>300</price> 
      <imagefilename>home4.gif</imagefilename> 
      <visits>4</visits> 
      </aproperty>
    - <aproperty>
      <postcode>ha4678</postcode> 
      <price>200</price> 
      <imagefilename>home5.gif</imagefilename> 
      <visits>5</visi开发者_如何学Gots> 
      </aproperty>
      </allproperty>

i want to write a php script that outputs this data without the tags, im not worried about the formatting, just the output, cheers thanks p.s. im using simplexml

EDIT:

is this wrong or right:

<?php

$fp = fopen('data_out.xml', 'r'); 
echo preg_replace('/<[^>]+>/', '', $fp);

?> 


There's actually a built in PHP function for stripping tags called...strip_tags().

echo strip_tags($xml_string);

No need to remember the regex!

EDIT: Just for posterity, here's the output for your sample XML http://ideone.com/hQS4P


This is massive overkill for what you're trying to do right now, but if you want to enhance it to format your data more nicely later, you may want to consider XSLT. This produces what you are looking for right now:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:template match="text()">
        <xsl:variable name="clean" select="normalize-space(.)"/>
        <xsl:if test="string-length($clean) > 0">
            <xsl:value-of select="$clean"/><xsl:text>&#10;</xsl:text>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>


If you just want to strip all tags, and if the XML is fully valid, you can use a regex.

Replace <[^>]+> with ''.

If the XML might have > characters in attributes, this won't work; you'd need a more complicated regex which is aware of strings.

EDIT: For example:

echo preg_replace('/<[^>]+>/', '', $someXML);
0

精彩评论

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