DOM解析XML

xiaoxiao2026-04-12  6

package com.netunit.workbench.util;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 何明 * */public class HandleSlotConfig { private static final String path = "D:\\slotconfig.xml"; private static Element root = null; private static Element theHandle = null; private static Element theProperties = null; public HandleSlotConfig(){ } /** * 将配置信息保存到本地 * @param timeSlot 时隙 * @param passage 通道 * @param address1 卡地址 * @param address2 槽道地址 * @param port1 源端 * @param port2 宿端 * @return */ public static int toWrite(String timeSlot,String passage,String address1,String address2,String port1,String port2){ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try{ DocumentBuilder db = factory.newDocumentBuilder(); File file = new File(path); Document doc = db.parse(file); root = doc.getDocumentElement(); theHandle = doc.createElement("info"); theProperties = doc.createElement("timeslot"); theProperties.setAttribute("id", timeSlot); theProperties.setTextContent(passage); theHandle.appendChild(theProperties); theProperties = doc.createElement("address"); theProperties.setAttribute("id", address1); theProperties.setTextContent(address2); theHandle.appendChild(theProperties); theProperties = doc.createElement("port"); theProperties.setAttribute("id", port1); theProperties.setTextContent(port2); theHandle.appendChild(theProperties); root.appendChild(theHandle); HandleLogXML.saveXml(path, doc); }catch(Exception e){ e.printStackTrace(); } return 0; } /** * 获得节点值 * @param key * @return */ public static String[] getMessage(String key){ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try{ DocumentBuilder db = factory.newDocumentBuilder(); File file = new File(path); Document doc = db.parse(file); String [] str = new String[doc.getDocumentElement().getElementsByTagName(key).getLength()]; NodeList le =doc.getDocumentElement().getElementsByTagName(key); for(int i = 0; i < le.getLength(); ++i){ Node no = le.item(i); str[i] = no.getTextContent(); } return str; }catch(Exception e){ e.printStackTrace(); } return null; } /** * 获得节点属性值 * @param key * @return */ public static String[] getAtributeValue(String key){ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try{ DocumentBuilder db = factory.newDocumentBuilder(); File file = new File(path); Document doc = db.parse(file); String[] str = new String[doc.getDocumentElement().getElementsByTagName(key).getLength()]; NodeList le = doc.getDocumentElement().getElementsByTagName(key); for(int i = 0; i < le.getLength(); ++i){ Element no = (Element)le.item(i); str[i] = no.getAttribute("id"); } return str; }catch(Exception e){ e.printStackTrace(); } return null; } public static void main(String []args){ HandleSlotConfig.toWrite("5", "8","3","5","3","2"); String[] str = new String[HandleSlotConfig.getMessage("port").length]; str = HandleSlotConfig.getMessage("port"); for(int i = 0; i < str.length; ++i){ System.out.println(str[i]); } String[] s = new String[HandleSlotConfig.getAtributeValue("address").length]; s = HandleSlotConfig.getAtributeValue("address"); for(int i = 0; i < s.length; ++i){ System.out.println(s[i]); } }}
转载请注明原文地址: https://www.6miu.com/read-5047234.html

最新回复(0)