4 XML DOM Document Object Model XML xml
Лекция 4 XML DOM (Document Object Model)
XML документ (пример) <? xml version="1. 0"? > <library name=". NET Developer's Library"> <book> <title>Programming Microsoft. NET</title> <author>Jeff Prosise</author> <isbn>0 -7356 -1376 -1</isbn> </book> <title>Microsoft. NET for Programmers</title> <author>Fergal Grimes</author> <isbn>1 -930110 -19 -7</isbn> </book> </library>
Основные Свойства и Методы DOM import javax. xml. parsers. Document. Builder; import javax. xml. parsers. Document. Builder. Factory; import java. io. File; import org. w 3 c. dom. Document; public class Order. Processor { public static void main (String args[]) { File doc. File = new File("orders. xml"); Document doc = null; try { Document. Builder. Factory dbf = Document. Builder. Factory. new. Instance(); Document. Builder db = dbf. new. Document. Builder(); doc = db. parse(doc. File); } catch (Exception e) { System. out. print("Problem parsing the file: "+e. get. Message()); } } }
Рекурсия через многие уровни потомков (2) public class Order. Processor { private static void step. Through (Node start) { System. out. println(start. get. Node. Name()+ " = "+start. get. Node. Value()); for ( Node child = start. get. First. Child(); child != null; child = child. get. Next. Sibling()) { step. Through(child); } } public static void main (String args[]) { File doc. File = new File("orders. xml"); . . . System. out. println("There are "+children. get. Length() +" nodes in this документ. "); //STEP 4: Recurse this functionality step. Through(root); } }
Включение атрибутов (2). . . import org. w 3 c. dom. Named. Node. Map; . . . private static void step. Through. All (Node start) { System. out. println(start. get. Node. Name()+" = "+start. get. Node. Value()); if (start. get. Node. Type() == start. ELEMENT_NODE) { Named. Node. Map start. Attr = start. get. Attributes(); for (int i = 0; i < start. Attr. get. Length(); i++) { Node attr = start. Attr. item(i); System. out. println(" Attribute: "+ attr. get. Node. Name() +" = "+attr. get. Node. Value()); } } for (Node child = start. get. First. Child(); child != null; child = child. get. Next. Sibling()) { step. Through. All(child); } }
Изменение значения узла (2) public class Order. Processor { private static void change. Order (Node start, String elem. Name, String elem. Value) { if (start. get. Node. Name(). equals(elem. Name)) { start. get. First. Child(). set. Node. Value(elem. Value); } for (Node child = start. get. First. Child(); child != null; child = child. get. Next. Sibling()) { change. Order(child, elem. Name, elem. Value); } } public static void main (String args[]) { // Change text content change. Order(root, "status", "processing"); Node. List orders = root. get. Elements. By. Tag. Name("status"); for (int order. Num = 0; order. Num < orders. get. Length(); order. Num++) { System. out. println(orders. item(order. Num). get. First. Child(). get. Node. Value()); } } }
Добавление узлов в документ (2) String total. String = new Double(total). to. String(); Node total. Node = doc. create. Text. Node(total. String); Element total. Element = doc. create. Element("total"); total. Element. append. Child(total. Node); this. Order. insert. Before(total. Element, this. Order. get. First. Child());
Удаление узла из документа (2) Element this. Order. Item = (Element)order. Items. item(item. Num); if (this. Order. Item. get. Attribute. Node("instock"). get. Node. Value(). equals("N")) { Node dead. Node = this. Order. Item. get. Parent. Node(). remove. Child(this. Order. Item); }
Замена узла if (this. Order. Item. get. Attribute. Node("instock"). get. Node. Value(). equals("N")) { Element back. Element = doc. create. Element("backordered"); Node dead. Node = this. Order. Item. get. Parent. Node(). replace. Child(back. Element, this. Order. Item); }
Создание и установка атрибутов if (this. Order. Item. get. Attribute. Node("instock“). get. Node. Value(). equals("N")) { Element back. Element = doc. create. Element("backordered"); back. Element. set. Attribute. Node(doc. create. Attribute("itemid")); String item. Id. String = this. Order. Item. get. Attribute. Node("itemid"). get. Node. Value(); back. Element. set. Attribute("itemid", item. Id. String); Node dead. Node = this. Order. Item. get. Parent. Node(). replace. Child(back. Element, this. Order. Item); }
Удаление атрибутов Element this. Order = (Element)orders. item(order. Num); Element customer = (Element)this. Order. get. Elements. By. Tag. Name("customerid“). item(0); customer. remove. Attribute("limit"); Node. List order. Items = this. Order. get. Elements. By. Tag. Name("item");
Почитать в Intenet про DOM http: //xerces. apache. org/xerces 2 -j/dom. html
- Slides: 32