Returns the rows in the given Table or Query as an XML-string, for example: <?xml version="1.0" encoding="utf-8"?>
(rows)
| 1390 | return "\"%s\"" % str(date(mktime(a.timetuple()))) |
| 1391 | |
| 1392 | def xml(rows): |
| 1393 | """ Returns the rows in the given Table or Query as an XML-string, for example: |
| 1394 | <?xml version="1.0" encoding="utf-8"?> |
| 1395 | <table name="pets", fields="id, name, type" count="2"> |
| 1396 | <schema> |
| 1397 | <field name="id", type="integer", index="primary", optional="no" /> |
| 1398 | <field name="name", type="string", length="50" /> |
| 1399 | <field name="type", type="string", length="50" /> |
| 1400 | </schema> |
| 1401 | <rows> |
| 1402 | <row id="1", name="Taxi", type="cat" /> |
| 1403 | <row id="2", name="Hofstadter", type="dog" /> |
| 1404 | </rows> |
| 1405 | </table> |
| 1406 | """ |
| 1407 | if isinstance(rows, Table): |
| 1408 | root, table, rows, fields, aliases = "table", rows, rows.rows(), rows.fields, {} |
| 1409 | if isinstance(rows, Query): |
| 1410 | root, table, rows, fields, aliases, = "query", rows.table, rows.rows(), rows.fields, rows.aliases |
| 1411 | fields = _unpack_fields(table, fields) |
| 1412 | # <table name="" fields="" count=""> |
| 1413 | # <query table="" fields="" count=""> |
| 1414 | xml = [] |
| 1415 | xml.append(XML_HEADER) |
| 1416 | xml.append("<%s %s=%s fields=\"%s\" count=\"%s\">" % ( |
| 1417 | root, |
| 1418 | root != "table" and "table" or "name", |
| 1419 | xml_format(table.name), # Use Query.aliases as field names. |
| 1420 | ", ".join(encode_entities(aliases.get(f,f)) for f in fields), |
| 1421 | len(rows))) |
| 1422 | # <schema> |
| 1423 | # Field information is retrieved from the (related) table schema. |
| 1424 | # If the XML is imported as a Table, the related fields become part of it. |
| 1425 | xml.append("\t<schema>") |
| 1426 | for f in fields: |
| 1427 | if f not in table.schema: |
| 1428 | s = f.split(".") |
| 1429 | s = table.db[s[0]].schema[s[-1]] |
| 1430 | else: |
| 1431 | s = table.schema[f] |
| 1432 | # <field name="" type="" length="" default="" index="" optional="" extra="" /> |
| 1433 | xml.append("\t\t<field name=%s type=%s%s%s%s%s%s />" % ( |
| 1434 | xml_format(aliases.get(f,f)), |
| 1435 | xml_format(s.type), |
| 1436 | s.length is not None and " length=%s" % xml_format(s.length) or "", |
| 1437 | s.default is not None and " default=%s" % xml_format(s.default) or "", |
| 1438 | s.index is not False and " index=%s" % xml_format(s.index) or "", |
| 1439 | s.optional is not True and " optional=%s" % xml_format(s.optional) or "", |
| 1440 | s.extra is not None and " extra=%s" % xml_format(s.extra) or "")) |
| 1441 | xml.append("\t</schema>") |
| 1442 | xml.append("\t<rows>") |
| 1443 | # <rows> |
| 1444 | for r in rows: |
| 1445 | # <row field="value" /> |
| 1446 | xml.append("\t\t<row %s />" % " ".join("%s=%s" % (aliases.get(k,k), xml_format(v)) for k, v in zip(fields, r))) |
| 1447 | xml.append("\t</rows>") |
| 1448 | xml.append("</%s>" % root) |
| 1449 | xml = "\n".join(xml) |
no test coverage detected