Home > other >  Can an XML file read data from a URL?
Can an XML file read data from a URL?

Time:12-11

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?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related