CMarkup Read and write XML

Source: Internet
Author: User

Fast start to XML in C + +

Enough bull. You want to the create XML or read and find things in XML. All your need to know about cmarkup are that it's just one object per XML document (for the API design concept see EDOM). And by the Firstobject XML Editor generates C + + source code for creating and navigating your own XML Documents with CMarkup.

Creating an XML Document

To create an XML document, instantiate a CMarkup object and call Addelem to create the root element. At this point your document would simply contain the empty root element e.g. <ORDER/> Then call Intoelem to go "inside" the ORDER element so it can create child elements under the root element (i.e. the The root element is the "container" of the child elements).

The following example code creates an XML document.

cmarkup Xml;xml. Addelem ("ORDER"); XML. Intoelem (); XML. Addelem ("ITEM"); XML. Intoelem (); XML. Addelem ("SN","132487a-j"); XML. Addelem ("NAME","Crank Casing"); XML. Addelem ("QTY","1");
  
This Code generates the following XML. The root is the ORDER element; Notice that it start tag  <order> is at the beginning and end tag  </order>  is at the bottom. When an element was under (i.e. inside or contained by) a parent element, the parent's start tag is before it and the Paren T ' s end tag is after it. The ORDER element contains one ITEM element. That ITEM element contains 3 child elements:sn, NAME, and QTY.
<ORDER><ITEM><SN>132487a-j</SN><NAME>Crank Casing</NAME><QTY>1</QTY></ITEM></ORDER>

As shown in the example, you create elements under a element by calling Intoelem to make your current main position (or " Place holder ") to your current parent position so you can begin adding child elements. CMarkup maintains a current position in order to keep your source code shorter and simpler. This same position logic was used when navigating a document.

You can write the above document to file with Save:

" C:\\sample.xml " );

And you can retrieve the XML to a string with Getdoc:

Mcd_str strxml = XML. Getdoc ();

Markup.h defines mcd_str to the string type you compile cmarkup for, so we use in MCD_STR these examples, but can use yo ur own string type explicitly (e.g. std::string or CString ).

Navigating an XML Document

You can navigate the data right inside the same CMarkup object you created in the example above; Just call Resetpos if you want to go back to the beginning of the document. Or you can populate a new CMarkup object:

CMarkup XML;

From a file with Load:

" C:\\sample.xml " );

Or from a XML string with Setdoc:

Xml. Setdoc (strxml);

In the following example, we go inside the root ORDER element and loop through all ITEM elements with Findelem to get the Serial number and quantity of each with GetData. The serial number is treated as a string and the quantity are converted to an integer using atoi (Mcd_2pcsz are defined in Markup.h to return the string ' s const pointer).

Xml. Findelem ();//root ORDER elementXml. Intoelem ();//inside ORDER while(XML. Findelem ("ITEM") {XML.    Intoelem (); Xml. Findelem ("SN" ); Mcd_str STRSN=XML.    GetData (); Xml. Findelem ("QTY" ); intNqty =atoi (Mcd_2pcsz (XML.    GetData ())); Xml. Outofelem ();}

For each of the item we find, we call Intoelem to interrogate it child elements, and then outofelemafterwards. As you get accustomed to this type of navigation you'll know to check in your loops to make sure there are a Correspondin G Call to OutOfElem every call IntoElem .

Adding Elements and Attributes

The above example for creating a document is only created one ITEM element. Here's an example this creates multiple items loaded from a previously populated data source, plus a SHIPMENT information element in which one of the elements have an attribute we set with Setattrib.

cmarkup Xml;xml. Addelem ("ORDER"); XML. Intoelem (); //inside ORDER for(intnitem=0; Nitem<aitems.getsize (); ++nitem) {XML. Addelem ("ITEM" ); Xml. Intoelem (); //inside ITEMXml. Addelem ("SN", AITEMS[NITEM].STRSN); Xml. Addelem ("NAME", Aitems[nitem].strname); Xml. Addelem ("QTY", Aitems[nitem].nqty); Xml. Outofelem (); //Back off to ITEM level}xml. Addelem ("SHIPMENT"); XML. Intoelem (); //inside SHIPMENTXml. Addelem ("POC"); XML. Setattrib ("type", strpoctype); XML. Intoelem (); //inside POCXml. Addelem ("NAME", strpocname); XML. Addelem ("TEL", Strpoctel);

This code generates the following XML. The root ORDER element contains 2 ITEM elements and a SHIPMENT element. The ITEM elements both contain SN, NAME and QTY elements. The SHIPMENT element contains a POC element which have a type attribute, and NAME and TEL child elements.

<ORDER><ITEM><SN>132487a-j</SN><NAME>Crank Casing</NAME><QTY>1</QTY></ITEM><ITEM><SN>4238764-a</SN><NAME>Bearing</NAME><QTY>15</QTY></ITEM><SHIPMENT><POCtype= "Non-emergency"><NAME>John Smith</NAME><TEL>555-1234</TEL></POC></SHIPMENT></ORDER>

Finding Elements

The FindElem method goes to the next sibling element. If the optional tag Name argument is specified and then it goes to the next element with a matching tag name. The element is found becomes the current element, and the next call to would FindElem go to the next sibling or matching Sibling after the current position.

When you cannot assume the order of the elements, you must move the position back before the first sibling with re Setmainpos in between your calls to the  Findelem  method. Looking at the ITEM element in the above example, if someone else was creating the XML and you cannot assume the SN element is before the QTY element and then call  resetmainpos  before Finding the QTY element.

 {XML.    Intoelem (); Xml. Findelem (  sn   " ); Mcd_str strsn  = XML.    GetData (); Xml.    Resetmainpos (); Xml. Findelem (  qty   " );  int  nqty = Atoi (Mcd_2pcsz (XML.    GetData ())); Xml. Outofelem ();}  
  
to Find the item with a particular serial number, you can loop through the item elements and compare the SN element data to The serial number is searching for. By specifying the "ITEM" element tag name in the  findelem   Method we ignore all other sibling elements such as the SHIPMENT element. Also, instead of going into and out of the ITEM element to look for the SN child element, we use the  findchild Elem getchilddata methods for convenience.
 XML. Resetpos (); //  top of document  XML. Findelem (); //  ORDER element is root  XML. Intoelem (); //  inside ORDER  while  (XML. Findelem ( item   "   sn   " );  if  (XML. Getchilddata () == Strfindsn)  break ; //  Found } 

You aren't on your own

This site have all kinds of examples of doing various XML operations. CMarkup have been widely used for many years. Of course it doesn ' t do everything, but almost every purpose have at least been discussed. Don ' t hesitate to ask if you have questions. A good place-to-go next is the CMarkup Methods.

CMarkup read/write XML (GOTO)

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.