一道面试题

We are hiring intermediate or experienced Java/Jscript programmers right now. And we hope you can demonstrate your experience/learning ability by solving the problem below. It'll be a jscript function to transform a given format of XML input to another XML format.
 
The input would be like:  

 

xml 代码
  1. <!---->xml version="1.0"?>    
  2. <menu xmlns="">    
  3.     <menuitem>    
  4.         <node>1node>    
  5.         <parent>1parent>    
  6.         <name>parentname>    
  7.     menuitem>    
  8.     <menuitem>    
  9.         <node>2node>    
  10.         <parent>1parent>    
  11.         <name>1st childname>    
  12.     menuitem>    
  13.     <menuitem>    
  14.         <node>3node>    
  15.         <parent>1parent>    
  16.         <name>2nd childname>    
  17.     menuitem>    
  18.     <menuitem>    
  19.         <node>4node>    
  20.         <parent>2parent>    
  21.         <name>grantchildname>  
  22.    <attr1>helloattr1>  
  23.    <attr2>Worldattr2>  
  24.     menuitem>    
  25. menu>   

 
The output would be like: 
 
xml 代码
  1. <!---->xml version="1.0"?>    
  2. <menu xmlns="">    
  3.     <menuitem>    
  4.         <name>parentname>    
  5.         <menuitem>    
  6.             <name>1st childname>    
  7.             <attr1>helloattr1>  
  8.             <attr2>Worldattr2>  
  9.             <menuitem>    
  10.                 <name>grand childname>    
  11.             menuitem>    
  12.         menuitem>    
  13.         <menuitem>    
  14.             <name>2nd childname>    
  15.         menuitem>    
  16.     menuitem>    
  17. menu>   


 
In the input format, tag "node" and "parent" specifies the structure of the tree, while "name" and others tags should be copied to the output format.  Therefore, the code should be able to handle the following fragment of XML:

 

xml 代码

 

  1. <menuitem>    
  2.         <node>2node>    
  3.         <parent>1parent>    
  4.         <name>1st childname>    
  5.         <lastName>XialastName>    
  6.         <firstName>HaofirstName>    
  7. menuitem>  


  For information on how to manipulate XML with jscript, you can check out MSDN's relevant section.

你可能感兴趣的:(xml,面试)