Simple XML Problem for you GURUs

I have a simple xml document, as such:

<?xml version="1.0" encoding="iso-8859-1"?>
<account name="myName 1" login="myLogin1" />
<account name="myName 2" login="myLogin2" />
<account name="myName 3" login="myLogin3" />
<account name="myName 4" login="myLogin4" />
<account name="myName 5" login="myLogin5" />

And a simple script, as follows:

set xml_source to ((path to desktop) as string) & "login.xml"
set xml_data to parse XML (read file xml_source)

With a simple error, like this:

XML parsing error: junk after document element at character 0 of line 3

Followed by a simple question, like so:

WTF?

Thanks,
-Warhaven

I’m not sure I understand what the question is or what you are asking. Plus, the second line of code will not complie on my machine running Panther.

set xml_source to ((path to desktop) as string) & "login.xml" 
set xml_data to parse XML (read file xml_source)

this will and it will read the file...

set xml_source to ((path to desktop) as string) & "login.xml"
set xml_data to (read file xml_source)

What are you trying to get AppleScript to do for you?

This works for me:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

I still get an error on the line that has…

set xml_data to parse XML xml_source

error = “Expected end of line, etc. but found identifier.” with the word XML highlighted. Are you all using an Osax for this?

Sorry, Greg, yes, this syntax is from the XML Tools OSAX from Late Night Software.

Jon

Yeah, it works if I add put it in the script it self, but I want to read it from an external file, then parse it. And I get an errror while trying to parse the read file.

Oh wait…let me try reading each paragraph rather than simply reading it. Hrmm, won’t be able to get to it 'til after the holidays though. Will be in Vegas.

Let me know if you can get it to work with an external file.

Thanks a ton,
-Rob

p.s. My question was: How can I get it to read an XML doc then parse the info? I’m getting the afforementioned error.

Saving this as a text file on my desktop with the name “login.xml”:

then using your original code:

works fine for me. Here is the result:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Odd…

Got back from Vegas, and it works now. Haven’t changed anything, but the error has suddenly stopped. :confused:

Oh well. Thanks for the help. I guess my computer just needed a vacation from me.

-Warhaven

Aha. I see what the problem is. My XML code is fine. The doctype is automatically generated on output, so you don’t need to actually include it in your xml file, and for some reason, the parser wants a parent for the whole document.

Should be:

<?xml version="1.0" encoding="iso-8859-1"> However, doctype automatically generated, and since it wants a parent for the entire document, I can just use: Thanks for the help, -Warhaven

It’s parsing everything fine, but I can’t seem to extract the values from it. I looked around for someone else with this problem, but couldn’t seem to find any helpful info. So, would someone help me with this quick bit?

Here’s what I have:

on awake from nib theObject
	set xml_source to ((path to desktop) as string) & "login.xml"
	set xml_data to parse XML (read file xml_source)
	repeat with myElement in XML contents of xml_data
		if XML tag of anItem is "account" then
			display dialog |name| of myElement as string
		end if
	end repeat
end awake from nib

Just a little script to pop up a window and show the value of |name|. Here’s the XML doc it’s reading from:

<login>
    <account name="myName 1" login="myLogin1"/>
    <account name="myName 2" login="myLogin2"/>
    <account name="myName 3" login="myLogin3"/>
    <account name="myName 4" login="myLogin4"/>
    <account name="myName 5" login="myLogin5"/>
</login>

Thanks,
-Warhaven

Hi Warhaven,

The code you posted did not define “anItem” in the repeat loop, try this instead:

[Ben]


[This script was automatically tagged for color coded syntax by Script to Markup Code]

Also it appeared the XML parser choked on the leading spaces of your posted XML, changing those spaces to tabs resolved the problem for me.

Ben

sigh

My bad. I’ll try and pay a little more attention to my code before wasting space on here. Sorry about that.

Thanks again,
-Warhaven