Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML parsing

Status
Not open for further replies.

Shukusei

Programmer
Sep 6, 2004
43
0
0
CA
As the topic might suggest, I'm having a bit of a hard time parsing some XML, also I'm new to PHP.


What I've been working with is Active-link.org's XML functions, as they seem to be the most streamlined and easiest for me to pick up at this point(much pressure to get something done quickly).


At anyrate, the problem is this:
When I try out the .php page, it shows nothing, and my script gets garbled up when viewed through the browser.
Here's a link: test.kithulu.com/test.php


As I said I'm compleatly new to PHP.


Here's the code for the test.php page:

Code:
<html>
	<head>
		<title>
			PHP Test
		</title>
	</head>
	<body>
		<?php
			require_once("classes/include.php");
			import("org.active-link.xml.XML");
			import("org.active-link.xml.XMLDocument"); 

			$journalObject = new XMLDocument("journal.xml");
			$journalXML = $journalObject->getXML();
			$journalEntries = $journalXML->getBranches("journalentry");			

			$journalEntries->arsort();
			$journalCount = count($journalEntries);
			
			//for ($i = 0; $i <= count($journalEntries); i++) {
		?>
			<table>
				<tr>
					<td>
						Subject: <?php
								//echo($journalEntries[1]->getTagContent("subject");
							?>
					</td>
					<td>
						Mood: <?php
								//echo($journalEntries[1]->getTagContent("mood");
							?>
					</td>
				</tr>
				<tr>
					<td>
						Music: <?php
								//echo($journalEntries[1]->getTagContent("music");
							?>
					</td>
				</tr>
				<tr>
					<td>
						<?php
							//echo($journalEntries[1]->getTagContent("entry");
						?>
					</td>
				</tr>
			</table>
		<?php
			}
		?>
	</body>
</html>

Any help'd be appreciated.
 
Are you sure the code you posted is in the test.phgp file?
There is absolutely NO output. The headers read:

Response Headers -
Date: Fri, 24 Dec 2004 04:13:17 GMT
Server: Apache/2.0.52 (Win32) PHP/5.0.2
X-Powered-By: PHP/5.0.2
Content-Length: 0
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=ISO-8859-1
 
Positive.

I've gotten it narrowed down to where the bug is, but I don't know how to fix it.

Code:
$journalCount = count($journalEntries);
            
            //for ($i = 0; $i <= count($journalEntries); i++) {


It's the for loop, but I don't know what's wrong with it...

I've tried changing it a bit and it still doesn't work.

Code:
$journalCount = sizeof($journalEntries);
			
			for ($i = 0; $i <= $journalCount; i++) {

The Active-link IORCA help files say that getBranches returns an array, so I figured I'd get the length of said array to loop through it, but it seems that either it's not getting the length, or my loop is messed up.

Like I said; I'm new to PHP.
 
Try to print out the array for inspection using preint_r(), the recursive printing function. Look at the source code of the page then to inspect it.

Also, for iterating arrays I would recommend the foreach($myarray as $key=>$value){ etc...} construct.
 
That is: print_r(), not preint_r()

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
I hate these wireless keyboards. While you type they swallow up things and later puke them out where they dont belong.
Thanks chessbot.
 
print_r($journalEntries); is giving me nothing... Maybe the array is messing up?

Shouldn't it at least be printing an empty array?
 
If it doesn't print anything then the variable is not even initialized.
 
So now to figure out why it's not setting anything into the variable....

I'm going through the help files, just to double check I did what I was supposed to do...
 
Aha! I got it, I messed up a bit in the search query for what I was looking for..

I tried using foreach, and it looped through successfully, though it ended up crashing when I uncommented the echo statements(I changed them to print when I uncommented).

I then tried using the regular for expression, and well.. It just kept going, and not stopping.
 
I'm not sure how I'm going to work this....

shows the value of the $journalEntries variable, and as you can see it's giving me *every* value, including the ones used for constructing the XML tree.

Now, I'm not sure how to get it to loop through so I can have easy access to the content I need(subject, date, entry).

Any ideas?

The Active-link IORCA files are under
 
Suggestion:
In order to make the output readable, echo "<pre>" before and "</pre>" after the print_r. Example:
Code:
$arr = array(1,2,"a");
print_r($arr)
// ------
Array ( [0] => 1 [1] => 2 [2] => "a")
// ...
echo "<pre>";
print_r($arr);
echo "</pre>";
// -------
Array
(
    [0] => 1
    [1] => 2
    [2] => "a"
)

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Okay, output should be understandable now. And yeah, it's most definatly giving me *everything* about the XML tree....


Is there an easy way to sift through it so that I only get the info I need; ie: entries?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top