Log in

View Full Version : Ask: Parsing XML Dengan Java


SinggahDapur
20th November 2011, 02:15 AM
Saya mempunya file xml dengan nama wewe.xml, sourcecodenya seperti dibawah ini.




Code:











Dalam sourcecode di atas terdapat tag berjumlah 6, dimana jumlah tag dengan nilai attribute kota="malang" () berjumlah 4 dan kota="madiun" () berjumlah 2.




Code:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
*
* @author rauff
*/
public class NewClass {

String AlamatFile = "wewe.xml";

public void parseXML() throws Exception {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File(AlamatFile));

NodeList nodeLst = doc.getElementsByTagName("map");
nodeLst.getLength();

System.out.println("Jumlah tag = "+nodeLst.getLength());
System.out.println("Jumlah tag = ");
System.out.println("Jumlah tag = ");
}

public static void main(String args[]) throws Exception {
NewClass nc = new NewClass();
nc.parseXML();

}
}

Output program


Quote:





run:

Jumlah tag = 6

Jumlah tag =

Jumlah tag =

BUILD SUCCESSFUL (total time: 0 seconds)





Bagaimana caranya mendapatkan total tag dan tag ?



Please bantu aku klo blum kelar aku gak lulus kuliah

</div>