Forum Discussion

FML2010's avatar
FML2010
Visitor
15 years ago

xml question if blank ??

what can i put in case the xml is blank?

for example

<hello></hello>

I tried

if e.GetName() = "hello" then
hello = e.GetBody()
end if
next

if hello = "" then
hello = "hello"
end if


if its empty it tosses up errors, so what can be put in its place so that doesnt happen? this above doesnt work either still shows errors as if its empty

thanks!

6 Replies

  • renojim's avatar
    renojim
    Community Streaming Expert
    Try, if hello = invalid, or if e = invalid.

    -JT
  • For debugging, I find a sprinkling of "print variable" "print type(variable)" (where "variable" is the actual variable) are useful. Just check for the output on the debugging console, and optionally prefix the prints with some identifying text if it's not obvious what's printing when.
  • thanks Jim I will try that.

    will that allow me to add what ever i want it to equal to?

    wont be able to test it till later tonight
  • He's referring to the value testing portion of your example code, where you are testing with

    if hello = ""


    he is implying that e.GetBody() may be returning the invalid object, and not a string at all. In which case you would wan to test for that. From a language standpoint, invalid is not equivalent to the empy string (""), so you need to test for it explicitly if it is possible it might be returned instead.

    P.S.
    It's generally possible to intuit what type of errors you might get from a sample piece of code if given enough information, but if you are getting error, include what they are. Anything that reduces the amount of work required for someone to help will increase the chance of getting a prompt, correct answer, and not a request for more information or an incorrect assumption about what the problem is. A bit of sample code with lots of print statements printing the state of a current variable (in this case the hello and e variables) before and after any point that might cause the problem, and including the debug output, will make helping much easier. For example, I would have rewritten (and run) the sample code you included as the following (assuming everything but "next" was code):


    print "e is ";type(e)
    print "e.GetName() is ";e.GetName()
    if e.GetName() = "hello" then
    hello = e.GetBody()
    end if

    print "hello is ";hello
    if hello = "" then
    hello = "hello"
    end if
    print "hello is now ";hello


    to which the console output might be (assuming it actually is getting invalid, I don't know)

    e is roXMLElement
    e.GetName() is hello
    hello is invalid
    hello is now invalid


    At which point the problem would be somewhat obvious, if this is the actual output.

    Of course, you don't have to be quite that pedantic when posting here, but in adding statements like that (which you would want to remove after identifying and fixing the problem, console prints have a specific performance penalty in my experience) you help illuminate why the code isn't functioning as you expect, in many cases allowing you to figure out and fix the problem without even having to ask how.

    The print statement is your friend.
  • I know it may be empty at times, so if does come back empty i dont want it to throw errors i want it to add something. it can be just " "

    or a phrase saying its empty like i posted originally
  • renojim's avatar
    renojim
    Community Streaming Expert
    If it is empty at times, you can test for it with either of these:
    if hello = ""
    if hello.Len() = 0

    Note that both statements will throw an error if hello is in fact invalid.

    -JT