xml parsing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gert Mellak

    xml parsing

    Hi, NG!

    I've spent many hours trying to understand why the dom-functions in
    php do this and that, but the following I can't understand:

    My xml-file looks like:

    <Data>
    <Article>
    <Number>3746</Number>
    <Description>Th is is the article description</Description>
    <Groups>
    <Group1>Group 1</Group1>
    <Group2>Group 2</Group2>
    <Group3>Group 3</Group3>
    </Groups>
    </Article>
    </Data>

    having the current node "Groups", I call

    //-- snip
    $childNodes = $curNode->child_nodes( );
    echo "Number of child-nodes: " . count($childNod es) . "<br>";
    //-- /snip

    The output is: "Number of child-nodes: 7"

    When I echo the node_name() of each child-node, every second node is
    called #text

    Could anyone of you explain why php reacts like this? I would expect a
    child-count of three and definitely no #text-nodes...

    Thanks in advance,

    Gert.
  • Zurab Davitiani

    #2
    Re: xml parsing

    Gert Mellak wrote:
    [color=blue]
    > <Groups>
    > <Group1>Group 1</Group1>
    > <Group2>Group 2</Group2>
    > <Group3>Group 3</Group3>
    > </Groups>[/color]

    <snip>
    [color=blue]
    > $childNodes = $curNode->child_nodes( );
    > echo "Number of child-nodes: " . count($childNod es) . "<br>";
    > //-- /snip
    >
    > The output is: "Number of child-nodes: 7"
    >
    > When I echo the node_name() of each child-node, every second node is
    > called #text
    >
    > Could anyone of you explain why php reacts like this? I would expect a
    > child-count of three and definitely no #text-nodes...[/color]

    The whitespace is counted as text nodes. If you had the whole thing on one
    line without any whitespace, it should be as you were expecting. I don't
    like DOM for many reasons, but that's another topic.

    Comment

    • Berislav Lopac

      #3
      Re: xml parsing

      Gert Mellak wrote:[color=blue]
      > Hi, NG!
      >
      > I've spent many hours trying to understand why the dom-functions in
      > php do this and that, but the following I can't understand:
      >
      > My xml-file looks like:
      >
      > <Data>
      > <Article>
      > <Number>3746</Number>
      > <Description>Th is is the article description</Description>
      > <Groups>
      > <Group1>Group 1</Group1>
      > <Group2>Group 2</Group2>
      > <Group3>Group 3</Group3>
      > </Groups>
      > </Article>
      > </Data>
      >
      > having the current node "Groups", I call
      >
      > //-- snip
      > $childNodes = $curNode->child_nodes( );
      > echo "Number of child-nodes: " . count($childNod es) . "<br>";
      > //-- /snip
      >
      > The output is: "Number of child-nodes: 7"
      >
      > When I echo the node_name() of each child-node, every second node is
      > called #text
      >
      > Could anyone of you explain why php reacts like this? I would expect a
      > child-count of three and definitely no #text-nodes...[/color]

      The whitespace (i.e. the newline char) between e.g. </Group1> and <Group2>
      is considered text.

      Berislav

      --
      If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
      Groucho, Chico, and Harpo, then Usenet is Zeppo.


      Comment

      • Janwillem Borleffs

        #4
        Re: xml parsing

        Gert Mellak wrote:[color=blue]
        > Could anyone of you explain why php reacts like this? I would expect a
        > child-count of three and definitely no #text-nodes...
        >[/color]

        In addition to Berislav's reply;

        You can stop this behaviour by using the DOMXML_LOAD_DON T_KEEP_BLANKS
        constant as the second argument of the domxml_open_fil e() call.


        JW



        Comment

        Working...