Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Manipulate XML Content the Ximple Way  : Page 6

For many common use cases, you can improve your XML-processing performance by taking advantage of XML-VTD's document-centric processing model.


advertisement
Example 3: Make an XML Template
An XML template is analogous to the tax forms you can pick up from the IRS. Those forms have descriptions of fields, but the fields themselves are empty and wide enough for you to fill in data. An XML template is basically an XML document with all the element tags, attribute names, and unfilled fields wide enough to fill in the data. Your application can parse the template, fill in the data, or do something else. Note that the data you fill in with this example becomes available to calling applications immediately, without the need to reparse.

This example takes a CD catalog document and replaces the text nodes with empty fields of defined lengths. Notice that this technique is similar to designing a SQL table, where you have to specify the width of the table columns.

Input XML Output XML
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>   <CD>
    <TITLE>Still Got the Blues</TITLE>
    <ARTIST>Gary More</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Virgin Records</COMPANY>
    <PRICE>10.20</PRICE>
    <YEAR>1990</YEAR>
  </CD>
  <CD>
    <TITLE>Hide Your Heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>
       CBS Records
    </COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>   <CD>
    <TITLE>Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
  </CD>
</CATALOG>

<CATALOG>
  <CD>
   <TITLE>                    </TITLE>
   <ARTIST>                  </ARTIST>
   <COUNTRY>       </COUNTRY>
   <COMPANY>      </COMPANY>
   <PRICE>         </PRICE>
   <YEAR>        </YEAR>
 </CD>
  <CD>
   <TITLE>                    </TITLE>
   <ARTIST>                  </ARTIST>
   <COUNTRY>       </COUNTRY>
   <COMPANY>      </COMPANY>
   <PRICE>         </PRICE>
   <YEAR>        </YEAR>
 </CD>
  <CD>
   <TITLE>                    </TITLE>
   <ARTIST>                  </ARTIST>
   <COUNTRY>       </COUNTRY>
   <COMPANY>      </COMPANY>
   <PRICE>         </PRICE>
   <YEAR>        </YEAR>
 </CD>
  <CD>
   <TITLE>                    </TITLE>
   <ARTIST>                  </ARTIST>
   <COUNTRY>       </COUNTRY>
   <COMPANY>      </COMPANY>
   <PRICE>         </PRICE>
   <YEAR>        </YEAR>
 </CD>
</CATALOG>

Here's the Java code that drives the example:

import com.ximpleware.*; public class makeTemplate { public static byte[] ba0, ba1, ba2, ba3, ba4, ba5; public static void main(String[] args) throws Exception { ba0 = " ".getBytes(); ba1 = " ".getBytes(); ba2 = " ".getBytes(); ba3 = " ".getBytes(); ba4 = " ".getBytes(); ba5 = " ".getBytes(); VTDGen vg = new VTDGen(); BookMark bm = new BookMark(); AutoPilot ap = new AutoPilot(); XMLModifier xm = new XMLModifier(); ap.selectXPath("/CATALOG/CD"); if (vg.parseFile("old_cd.xml", false)) { VTDNav vn = vg.getNav(); ap.bind(vn); xm.bind(vn); int i; //Insert spaces which will later be edited on while ((i = ap.evalXPath()) != -1) { convert(vn,xm); } xm.output("cd_Template.xml"); } } public static void convert(VTDNav vn, XMLModifier xm) throws Exception{ int i = -1; vn.toElement(VTDNav.FIRST_CHILD); i = vn.getText(); xm.updateToken(i,ba0); vn.toElement(VTDNav.NEXT_SIBLING); i = vn.getText(); xm.updateToken(i,ba1); vn.toElement(VTDNav.NEXT_SIBLING); i = vn.getText(); xm.updateToken(i,ba2); vn.toElement(VTDNav.NEXT_SIBLING); i = vn.getText(); xm.updateToken(i,ba3); vn.toElement(VTDNav.NEXT_SIBLING); i = vn.getText(); xm.updateToken(i,ba4); vn.toElement(VTDNav.NEXT_SIBLING); i = vn.getText(); xm.updateToken(i,ba5); vn.toElement(VTDNav.PARENT); } }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap