acpilot
acpilot

Reputation: 141

Can an XML file read data from a URL?

I am creating a simple XML feed and would like to have the feed reference product quantity data from a URL.

Is it possible for an XML file to reference a value from a URL? If so, how can this be done?

Upvotes: 0

Views: 337

Answers (2)

imhotap
imhotap

Reputation: 2490

Generally speaking, you can declare external entities in the DOCTYPE and then reference those entities in content to pull in character from file or network resources and expand in place. In the following example, the entity reference &ent; is replaced by whatever is fetched from http://example.com/some-data if there would exist anything at that URL (which it doesn't):

<!DOCTYPE doc [
  <!ELEMENT doc (#PCDATA)>
  <!ENTITY ent
    SYSTEM "http://example.com/some-data">
]>
<doc>
  &ent;
</doc>

However, it depends on your XML parser/processor if it actually implements DOCTYPE processing and if it can fetch from http: URLs or file names. For example, Web browsers, when receiving XML or XHTML, won't fetch external content, whereas command line tools or document processors for XML and SGML can generally be expected to perform DTD parsing/validation and external entity expansion.

Upvotes: 2

Quentin
Quentin

Reputation: 943595

XML is a generic data format designed for other data formats (like SVG, XHTML, Atom, and MIX) to be built on top of it. Often by mixing and matching other such formats.

XML itself has no means to arbitrarily pull in data from somewhere.

A specific XML application (let's call it YourXFeed) might, and it might do that by making the reference with XLink. Then it would be up to applications designed to consume YourXFeed files to follow those links and pull the data from them into the resulting data structure they output when parsing your XML.

Alternatively, you could embed the data directly into your XML file on demand by using server-side programming.

Or you could do the same thing, but by generating static files on a schedule.

Upvotes: 1

Related Questions