473,382 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

XML parsing

Hello

I have been trying to wrap my head arouns this, but I can't seem to get it to
work the way I want it to. Could someone helpful please tell me how I get this:

<function>
<name>array_push</name>
<examples>
<example>
<name>A way to do it</name>
<code>
$array = array("foo", "bar")
$array = array_push($array, "goo")
</code>
</example>
</examples>
</function>

To this:

array (
name -> array_push
examples -> array(
example -> array(
name -> A way to do it
code -> $array = array("foo", "bar")
$array = array_push($array, "goo")
)
)
)

My problems so far has been traversing the xml in a good way and:

1. After parsing, I am left with the code stripped of variables
'$array = array("foo", "bar")' becomes ' = array("foo", "bar")'

2. Newlines in the code disappaers, how do I make them stay?

Anyone feeling overly helpful and could give me an example to make the above
xml into the above array.

--
Sandman[.net]
Jul 16 '05 #1
5 2557
In article <mr**********************@news.fu-berlin.de>,
Sandman <mr@sandman.net> wrote:
Hello

I have been trying to wrap my head arouns this, but I can't seem to get it to
work the way I want it to. Could someone helpful please tell me how I get
this:

<function>
<name>array_push</name>
<examples>
<example>
<name>A way to do it</name>
<code>
$array = array("foo", "bar")
$array = array_push($array, "goo")
</code>
</example>
</examples>
</function>

To this:

array (
name -> array_push
examples -> array(
example -> array(
name -> A way to do it
code -> $array = array("foo", "bar")
$array = array_push($array, "goo")
)
)
)

My problems so far has been traversing the xml in a good way and:

1. After parsing, I am left with the code stripped of variables
'$array = array("foo", "bar")' becomes ' = array("foo", "bar")'

2. Newlines in the code disappaers, how do I make them stay?

Anyone feeling overly helpful and could give me an example to make the above
xml into the above array.

Is there really no one who has a simple solution to the above?

--
Sandman[.net]
Jul 16 '05 #2
Sandman wrote on Monday 15 September 2003 00:38:
<function>
<name>array_push</name>
<examples>
<example>
<name>A way to do it</name>
<code>
$array = array("foo", "bar")
$array = array_push($array, "goo")
</code>
</example>
</examples>
</function>

To this:

array (
name -> array_push
examples -> array(
example -> array(
name -> A way to do it
code -> $array = array("foo", "bar")
$array = array_push($array, "goo")
)
)
)

My problems so far has been traversing the xml in a good way and:

1. After parsing, I am left with the code stripped of variables
'$array = array("foo", "bar")' becomes ' = array("foo", "bar")'

2. Newlines in the code disappaers, how do I make them stay?

Anyone feeling overly helpful and could give me an example to make the
above xml into the above array.

Is there really no one who has a simple solution to the above?


I didn't get your first post, just your own follow-up. Here's one solution:

1. Get ActiveLink PHP XML Package from:

http://www.active-link.com/intranet/software.php
OR
http://php-xml.sourceforge.net/

2. It helps to read docs at this point.

3. Parse your XML string into an XML object like:
$functionXML = new XML($functionXMLString);
OR, if you are reading from a file, you can use XMLDocument object for that
too.

4. Use something like following function to get your array (warning -
untested code):

function convertXMLToArray($xml) {
$arrNew = array();
if(get_class($xml) != "xml" && get_class($xml) != "xmlbranch")
return(false);
else {
if($xml->hasBranch()) {
$branches = $xml->getBranches();
foreach($branches as $branch) {
$tagName = $branch->getTagName();
if($branch->hasBranch())
$arrNew[$tagName] = convertXMLToArray($branch);
else
$arrNew[$tagName] = $branch->getTagContent();
}
}
else
$arrNew[] = $xml->getTagContent();
}
return($arrNew);
}

Note: If you specifically need an array for some reason, use the above; if
you don't need an array, but need a way to store, access, and modify XML,
then simply use XML object methods to do so, i.e. you won't need to mess
with arrays and the above function at all.

Good luck!

Disclaimer: I am the author of ActiveLink PHP XML Package; the package is
available under LGPL.

--
Business Web Solutions
ActiveLink, LLC
www.active-link.com/intranet/
Jul 16 '05 #3
Zurab Davitiani wrote:
Note: If you specifically need an array for some reason, use the above; if
you don't need an array, but need a way to store, access, and modify XML,
then simply use XML object methods to do so, i.e. you won't need to mess
with arrays and the above function at all.
What I really don't get is what he's going to do with the data. If he's
gonna print it, then XSL is the way to go, IMHO.
Disclaimer: I am the author of ActiveLink PHP XML Package; the package is
available under LGPL.


No harm in little self promotion. :) Nice job on the package.

--
Dado

Somewhere, just out of sight, the unicorns are gathering.

Jul 16 '05 #4
In article <9e************@amelie.ofr.hr>,
Dalibor Karlovic <da**@krizevci.info> wrote:
Note: If you specifically need an array for some reason, use the above; if
you don't need an array, but need a way to store, access, and modify XML,
then simply use XML object methods to do so, i.e. you won't need to mess
with arrays and the above function at all.


What I really don't get is what he's going to do with the data. If he's
gonna print it, then XSL is the way to go, IMHO.


Ok, please elaborate - any suggestions are welcome!

--
Sandman[.net]
Jul 16 '05 #5
Sandman wrote:

I appolagize for the length of the post.
What I really don't get is what he's going to do with the data. If he's
gonna print it, then XSL is the way to go, IMHO.


Ok, please elaborate - any suggestions are welcome!


You do like this:

<?php
// XML + XSL
$oXsl = xslt_create();

if ($sResult = xslt_process($oXsl, 'test.xml', 'test.xsl', NULL)){
echo $sResult;
} else {
die (xslt_error($oXsl));
}

xslt_free($oXsl);
?>

This will parse files test.xml and test.xsl which are located where the
script is located. The content of the files:

(test.xml)

<functions>
<function>
<name>array_push</name>
<examples>
<example>
<name>a way to do it</name>
<code>
<![CDATA[
$myArray = array("foo", "bar");
$myArray = array_push($myArray, "goo");
]]>
</code>
</example>
<example>
<name>another way</name>
<code>
<![CDATA[
$myArray = array("foo", "bar", "goo");
$myArray = array_push($myArray, "goo");
]]>
</code>
</example>
</examples>
</function>
<function>
<name>array_pop</name>
<examples>
<example>
<name>you use it like this (code doesn't work :)</name>
<code>
<![CDATA[
$myArray = array("foo", "bar");
$myArray = array_pop($myArray, "goo");
]]>
</code>
</example>
</examples>
</function>
</functions>

test.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="function">
<h1>
<xsl:value-of select="name"/>
</h1>
<xsl:for-each select="examples">
<xsl:for-each select="example">
<div>
<xsl:value-of select="name"/>
<pre>
<code>
<xsl:value-of select="code"/>
</code>
</pre>
</div>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Output I got when finished:

<?xml version="1.0" encoding="UTF-8"?>
<h1>array_push</h1><div>a way to do it<pre><code>

$myArray = array("foo", "bar");
$myArray = array_push($myArray, "goo");

</code></pre></div><div>another way<pre><code>

$myArray = array("foo", "bar", "goo");
$myArray = array_push($myArray, "goo");

</code></pre></div>
<h1>array_pop</h1><div>you use it like this (code doesn't work
:)<pre><code>

$myArray = array("foo", "bar");
$myArray = array_pop($myArray, "goo");

</code></pre></div>

I am _really_ new to all this XSL business so this probably can be done much
cleaner, but this works :)

--
Dado

There is no time like the pleasant.
Jul 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
0
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
5
by: randy | last post by:
Can some point me to a good example of parsing XML using C# 2.0? Thanks
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
7
by: Daniel Fetchinson | last post by:
Many times a more user friendly date format is convenient than the pure date and time. For example for a date that is yesterday I would like to see "yesterday" instead of the date itself. And for...
1
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.