XML etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
XML etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

10 Nisan 2018 Salı

NodeList Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.w3c.dom.NodeList;
constructor
Şöyle yaparız.
NodeList nodeList = doc.getElementsByTagName("student");
Şöyle yaparız.
Node channel = ...;
NodeList nodeList = channel.getChildNodes();
appendChild metodu
Örnek
Elimizde şöyle bir XML olsun.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<mapping-configuration>
  <fields-mapping>
    <field compare="true" criteria="true" displayName="demo1"/>
    <field compare="true" criteria="true" displayName="demo2"/>
  </fields-mapping>
</mapping-configuration>
Yeni bir düğüm eklemek için şöyle yaparız.
Document doc = builder.parse(new File("C:/Desktop/test.xml"));  

Node nodeList = doc.getDocumentElement().getChildNodes().item(0);

Element newserver=doc.createElement("field");
newserver.setAttribute("source", "33");
nodeList.appendChild(newserver).normalize();
Yeni XML şöyledir.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<mapping-configuration>
  <fields-mapping>
    <field compare="true" criteria="true" displayName="demo1"/>
    <field compare="true" criteria="true" displayName="demo2"/>
    <field source="33"/> 
  </fields-mapping>
</mapping-configuration>
getLength metodu
Liste şöyle dolaşılır.
NodeList nodeList = doc.getElementsByTagName("student");
for (int index = 0; index < nodeList.getLength(); index++) {
  Node node = nodeList.item(index);
  ...
}
item metodu
Node nesnesi döndürür. Şöyle yaparız.
NodeList nodeList = ...;
Node child = items.item(2);

Node Arayüzü

Giriş
Şu satırı dahil ederiz.
import org.w3c.dom.Node;
Node bir XML tag'i veya attribute bile olabilir.

appendChild metodu
Örnek
Şöyle yaparız.
Node child = ...;
node.appendChild (child);
getAttributes metodu
Şöyle yaparız.
NamedNodeMap attributes = node.getAttributes();
getChildNodes metodu
Örnek
Şöyle yaparız.
NodeList nodeList = node.getChildNodes();
Örnek
Elimizde şöyle bir XML olsun.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<mapping-configuration>
  <fields-mapping>
    <field compare="true" criteria="true" displayName="demo1"/>
    <field compare="true" criteria="true" displayName="demo2"/>
  </fields-mapping>
</mapping-configuration>
fields-mapping düğümüne erişmek için şöyle yaparız.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("C:/Desktop/test.xml"));  

Node nodeList = doc.getDocumentElement().getChildNodes().item(0);
getNodeName metodu
Düğümün tipi Element ise NodeName , Tag Name ile aynıdır. Düğümün tipi Text ise NodeName "#text" değeridir. Şöyle yaparız.
Node node = nodeList.item(index);
System.out.println("Current Node :" + node.getNodeName());
getNodeType metodu
Şöyle yaparız.
if (node.getNodeType() == Node.ELEMENT_NODE) {...}
Şöyle yaparız.
if (item.getNodeType() == Node.TEXT_NODE) {...}
getNodeValue metodu
Şöyle yaparız.
node.getNodeValue()
getTextContent metodu
Şöyle yaparız.
String textContent = node.getTextContent();
hasAttributes metodu
Şöyle yaparız.
if (node.hasAttributes()) {...}
hasChildNodes metodu
Şöyle yaparız.
if (node.hasChildNodes()) {...}
normalize metodu
Şöyle yaparız.Ayrı ayrı olan tüm text düğümlerini birleştirir.
node.normalize();
Doküman ilk yüklenirken kullanılır. Şöyle yaparız.
Document doc = ...
doc.getDocumentElement().normalize();