An easy way to merge multiple XML files-External entity references

Source: Internet
Author: User

The following example merges the file Order1.xml with the file order2.xml into a single file through an external entity reference Directory.xml

Listing 1. XML document Order1.xml to be merged:

<?xml version= "1.0" encoding= "GB2312"?>

<order>
  <item>
    <itemnumber>ksu-0384</itemnumber>
    <DESCRIPTION>LCD LCD monitor </description>
    <quantity>1</ Quantity>
    <price>358.00</price>
  </item>
     <item>
    <itemnumber>ksu-0511</itemnumber>
    <description> speaker </description>
    <quantity>1</quantity>
     <price>16.50</price>
  </item>
    <item>
    <itemnumber>ksu-7710</itemnumber>
    <description> Mouse < /description>
    <quantity>1</quantity>
    <price> 8.50</price>
  </item>
</order>

Listing 2. XML document to be merged order2.xml:

<?xml version= "1.0" encoding= "GB2312"?>

<order>
  <item>
    <itemnumber>ksu-0652</itemnumber>
    <description> threesome keyboard </description>
    <quantity>1</ Quantity>
    <price>35.00</price>
  </item>
     <item>
    <itemnumber>ksu-7720</itemnumber>
    <description>intel cpu</description>
    <quantity>1</quantity>
    <price>1650.00</price>
  </item>
    <item>
    <itemnumber>ksu-7010</itemnumber>
    <description> Maxtor HDD </description>
    <quantity>1</quantity>
    < Price>850.00</price>
  </item>
</order>

Listing 3. Output document Directory.xml, the boldface section is the entity declaration and its reference:

<?xml version= "1.0"?>
<! DOCTYPE Directory [
<! ENTITY disclaimer1 SYSTEM "./order1.xml" >
<! ENTITY disclaimer2 SYSTEM "./order2.xml" >
]>
<directory>
&disclaimer1;
&disclaimer2;

</directory>

When an XML 1.0-compliant processor reads a document, it expands the entity to replace the reference with its contents, so the complete document looks like the following.

Listing 4. Document display with extended entities (in IE6):

<?xml version= "1.0"?>
<! DOCTYPE directory (View Source for full DOCTYPE ...) >
-<directory>
-<order>
-<item>
<itemNumber>ksu-0384</itemNumber>
&LT;DESCRIPTION&GT;LCD LCD Monitor </description>
<quantity>1</quantity>
<price>358.00</price>
</item>
-<item>
<itemNumber>ksu-0511</itemNumber>
<description> Speaker </description>
<quantity>1</quantity>
<price>16.50</price>
</item>
-<item>
<itemNumber>ksu-7710</itemNumber>
<description> Mouse </description>
<quantity>1</quantity>
<price>8.50</price>
</item>
</order>
-<order>
-<item>
<itemNumber>ksu-0652</itemNumber>
<description> Threesome Keyboard </description>
<quantity>1</quantity>
<price>35.00</price>
</item>
-<item>
<itemNumber>ksu-7720</itemNumber>
<description>intel cpu</description>
<quantity>1</quantity>
<price>1650.00</price>
</item>
-<item>
<itemNumber>ksu-7010</itemNumber>
<description> Maxtor HDD </description>
<quantity>1</quantity>
<price>850.00</price>
</item>
</order>
</directory>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.