2016-07-22 9 views
1

Java-Code erforderlich für die folgende Operation. mit Im einem XML als Zeichenfolge, zum BeispielJava XML pharsing löschender Knoten

<EMpData> 
<Employee1> 
    <Address1>XYZ<Address1/> 
    <city>DDD<city/> 
<Employee1/> 
<Employee2> 
    <Address1><Address1/> 
    <city><city/> 
<Employee2/> 
<EMpData/> 

Ich habe zu prüfen, ob ein Kind Knoten leer oder null oder ohne Wert ist, wenn es da ist, dann muß ich den Eltern-Kind-Knoten löschen. So in oben employee2 leer ist, möchte ich die folgenden Ausgangs

<EMpData> 
<Employee1> 
    <Address1>XYZ<Address1/> 
    <city>DDD<city/> 
<Employee1/> 
<EMpData/> 

vorschlagen Bitte die beste und effizienteste Weg, dies zu tun. Die tatsächliche xml wird etwa 500 bis 700 Zeilen sein. Hier

+0

Was haben Sie versucht? –

Antwort

0

ist der Code auf Basis von XPath und VTD-XML ..

import java.io.*; 
import com.ximpleware.*; 

public class removeNode2 { 
    public static void main(String s[]) throws VTDException, IOException{ 
     VTDGen vg = new VTDGen(); 
     String xml = "<EMpData>\n"+ 
       "<Employee1>\n"+ 
       "<Address1>XYZ</Address1>\n"+ 
       "<city>DDD</city>\n"+ 
       "</Employee1>\n"+ 
       "<Employee2>\n"+ 
       "<Address1></Address1>\n"+ 
       "<city></city>\n"+ 
       "</Employee2>\n"+ 
       "</EMpData>\n"; 
     vg.setDoc(xml.getBytes()); 
     vg.parse(false); 
     VTDNav vn = vg.getNav(); 
     AutoPilot ap = new AutoPilot(vn); 
     XMLModifier xm = new XMLModifier(vn); 
     ap.selectXPath("/EMpData/*[string-length(*/text())=0]"); 

     int i=0; 
     while((i=ap.evalXPath())!=-1){ 
      //System.out.println(" -====>"+ap2.evalXPathToBoolean()); 
      xm.remove(); 
     } 
     ByteArrayOutputStream baos= new ByteArrayOutputStream(); 
     xm.output(baos); 
     System.out.println(baos.toString()); 
    } 
}