XmlHandle.java:
package shuai.study.xml; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; /** * @ClassName: ReadXml * @Description: TODO * @author Zhou Shengshuai * @date 2014年9月23日 下午5:09:38 * */ public class XmlHandle { public static void readXml(String filePath) { XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); XMLStreamReader xmlStreamReader = null; try { InputStream fileInputStream = new FileInputStream(filePath); xmlStreamReader = xmlInputFactory.createXMLStreamReader(new BufferedInputStream(fileInputStream,65536)); readXml(xmlStreamReader); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (XMLStreamException e) { e.printStackTrace(); } finally { if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (XMLStreamException e) { e.printStackTrace(); } } } } public static void readXml(XMLStreamReader xmlStreamReader) { try { while (xmlStreamReader.hasNext()) { int outType = xmlStreamReader.next(); if (outType == XMLStreamReader.START_ELEMENT && "Rule".equals(xmlStreamReader.getLocalName())) { System.out.println("---------------------------------------Rule Start---------------------------------------"); while (xmlStreamReader.hasNext()) { int inType = xmlStreamReader.next(); if (inType == XMLStreamReader.START_ELEMENT) { String elementText = xmlStreamReader.getElementText(); if ("SourceObjectClass".equals(xmlStreamReader.getLocalName())) { System.out.println("Source Object Class: " + elementText); } if ("DestObjectClass".equals(xmlStreamReader.getLocalName())) { System.out.println("Destination Object Class: " + elementText); } if ("ManageObjectClass".equals(xmlStreamReader.getLocalName())) { System.out.println("Manage Object Class: " + elementText); } if ("SourceObjectDN".equals(xmlStreamReader.getLocalName())) { System.out.println("Source Object DN: " + elementText); } if ("DestObjectDN".equals(xmlStreamReader.getLocalName())) { System.out.println("Destination Object DN: " + elementText); } } if (inType == XMLStreamReader.END_ELEMENT && "Rule".equals(xmlStreamReader.getLocalName())) { System.out.println("---------------------------------------Rule End---------------------------------------"); break; } } } if (outType == XMLStreamReader.END_ELEMENT && "Mapping".equals(xmlStreamReader.getLocalName())) { break; } } } catch (XMLStreamException e) { e.printStackTrace(); } } /** * @Title: main * @Description: TODO * @param args * @throws */ public static void main(String[] args) { XmlHandle.readXml("conf/dnMapping.xml"); } }
dnMapping.xml:
<?xml version="1.0" encoding="UTF-8"?> <Mapping> <Rule name="Rule00"> <SourceObjectClass>FING</SourceObjectClass> <DestObjectClass>GgsnFunction</DestObjectClass> <ManageObjectClass>3529</ManageObjectClass> <SourceObjectDN>PLMN-PLMN/FING-1</SourceObjectDN> <DestObjectDN>SubNetwork=XXXXX-1,ManagedElement=FING-1,GgsnFunction=FING-1</DestObjectDN> </Rule> <Rule name="Rule01"> <SourceObjectClass>FPIFC</SourceObjectClass> <DestObjectClass>EthernetPort</DestObjectClass> <ManageObjectClass>3529</ManageObjectClass> <SourceObjectDN>PLMN-PLMN/FING-1/FPNODE-1/FPIFC-1</SourceObjectDN> <DestObjectDN>SubNetwork=XXXXX-1,EthernetPort=FPNODE-1/FPIFC-1</DestObjectDN> </Rule> <Rule name="Rule10"> <SourceObjectClass>FING</SourceObjectClass> <DestObjectClass>EpRpDynS5S8Sgw</DestObjectClass> <ManageObjectClass>3529</ManageObjectClass> <SourceObjectDN>PLMN-PLMN/FING-1</SourceObjectDN> <DestObjectDN>SubNetwork=XXXXX-1,ServingGwFunction=FING-1,EpRpDynS5S8Sgw=S5S8-1</DestObjectDN> </Rule> </Mapping>原文链接:https://www.f2er.com/xml/297980.html