#1820 XDoc => write as String

Xan Wed 7 Mar 2012

Hi,

I use this example (citing in xml)

doc := XDoc
{
  XElem("root")
  {
    XElem("a") { addAttr("flags", "0"); XText("blah"), },
    XElem("b") { XElem("c"), },
    XElem("m") { XText("I "), XElem("i") { XText("mean"), }, XText(" it!"), },
  },
}
doc.write(Env.cur.out)

I want to modify it: replace doc.write(Env.cur.out) to doc.toStr But doc.toStr only writes:

"<?xml version='1.0'?>"

Why? How to get the doc as string?

Any suggestions?

Thanks in advance, Xan.

brian Wed 7 Mar 2012

The toStr is used for return just the start element to make it useful for debugging. If you want to write the whole document or XML element to a string:

buf := StrBuf()
xml.wrtie(buf.out)
return buf.toStr

Xan Thu 8 Mar 2012

Thank you very much, Brian. But in deep-discussion, is it natural? Why do you choose that xml.toStr contain only

"<?xml version='1.0'?>"

and not its whole contents as Str? If we espect that Object.toStr why a subobject (xml) does not behaves this way?

Thanks,

brian Thu 8 Mar 2012

If we espect that Object.toStr why a subobject (xml) does not behaves this way?

Actually convention is that all objects work this way where toStr provides a single line of debugging information but doesn't necessarily dump the full object if has many lines. Fpr example Err.toStr shows the type and msg, but doesn't return entire stack trace.

Login or Signup to reply.