An optimal scheme for XML log records files

Source: Internet
Author: User

XML as a way of data storage, when the data is very large, we will encounter a lot of XML processing problems. In general, the most straightforward way to edit an XML file is to load the XML file into XmlDocument, modify the XmlDocument in memory, and then save it to disk. In this way we will have to load the entire XML document into memory, which is obviously unwise (for large data XML files, memory will be very large, the elder brother said the pear is very big). The next thing we'll talk about is how to efficiently add content (object entity content) to an XML log file.

(i) Design overview

In general, we'll create two different files (through code), the first of which is an XML file, and the second is an XML fragment (TXT file), as shown in the following illustration:

We associate 2 different files with the following definition.

<! ENTITY yourentityrefname SYSTEM "Your XML fragement address (relative or Obsolute address)" >

(ii) generation of XML files

Let's take a look at how to create a related XML file with the following code:

 private static void Initxmlfile (String xmllogfilepath, String xmllogcontentfilename, String entityref) { String docType = String. Format ("\n<! DOCTYPE xmllogfile \ n [\ n <!
            ENTITY {0} SYSTEM \ "{1}\" >\n]>\n ", EntityRef, Xmllogcontentfilename);
            XmlWriterSettings wrappersettings = new XmlWriterSettings () {Indent = true}; using (XmlWriter writer = XmlWriter.Create (Xmllogfilepath, wrappersettings)) {Write
                R.writestartdocument (); Writer.
                WriteRaw (DocType); Writer.
    
                WriteStartElement (Configresource.xmllogfile); Writer.
                WriteStartElement (configresource.xmllogcontent); Writer.
                Writeentityref (ENTITYREF); Writer.
    
                WriteEndElement (); Writer.
                WriteEndElement (); Writer.
            Close (); }
        }

Writes to the contents of an XML file are performed primarily through XmlWriter. This method is relatively simple, no longer explained, look at the contents of the file we generated through this method:

<?xml version= "1.0" encoding= "Utf-8"?> <!
DOCTYPE xmllogfile 
 [ 
 <! ENTITY locations SYSTEM "Xmllogcontentfile-20110220000120.txt" >
 ]>
<XmlLogFile>
  < Xmllogcontent>&locations;</xmllogcontent>
</XmlLogFile>

Locations is the entity reference name, which corresponds to the &Locations; 。

Xmllogcontentfile-20110220000120.txt is the file name of the XML fragment, and the path is relative to the xmllogfile-20110220000120.xml.

&Locations; acts as placeholders, replacing Xmllogfile-20110220000120.xml & with the contents of the Xmllogcontentfile-20110220000120.txt file locations;

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.