-
導入jar包查看全部
-
四種解析方式DOM、SAX、DOM4J、JDOM查看全部
-
根節點 --》子節點查看全部
-
1.初次邂逅xml 2.在Java程序中如何獲取xml文件的內容 3.在Java程序中如何生成xml文件查看全部
-
節點類型查看全部
-
在JDOM中存儲對象 1.創建一個新的book類并將其實例化 Book bookEntity = new Book(); 2.添加節點屬性值到book if(attrName.equals("id")){ personEntity.setId(attrValue); } 3.添加子節點屬性值到book if (child.getName().equals("name")) { bookEntity.setName(child.getValue()); } else if (child.getName().equals("author")) { bookEntity.setAuthor(child.getValue()); } else if (child.getName().equals("year")) { bookEntity.setYear(child.getValue()); } else if (child.getName().equals("price")) { bookEntity.setPrice(child.getValue()); } else if (child.getName().equals("language")) { bookEntity.setLanguage(child.getValue()); }查看全部
-
JDOM解析亂碼處理 1.修改xml第一行的encoding屬性 2.在代碼中處理,應用IO流知識 //創建輸入流,將XML文件加載到輸入流中 InputStream in = new FileInputStream("src/book.xml"); //使用包裝流InputStreamReader進行讀取編碼的指定 InputStreamReader isr = new InputStreamReader(in,"UTF-8");查看全部
-
獲取節點名、值:getNodeName() getNodeValue() getTextContent() 獲取子節點:getChildNodes() 返回 NodeList 獲取屬性節點:getAttributes() 返回 NamedNodeMap JDOM 解析 獲取節點名、值:getName() getValue() 獲取子節點:getChildren() 返回 List<Element> 獲取屬性節點:getAttributes() 返回 List<Attribute>查看全部
-
//通過增強for循環進行遍歷子節點集合 for(Element person : personList){ System.out.print("======開始解析第" + personList.indexOf(person)+1 + "個人======"); //解析person的屬性 List<Attribute> attrList = person.getAttributes();//適用于我們不知道里面有多少屬性 person.getAttributeValue("id");//適用于我們知道子節點屬性的名字直接獲取其屬性值 //遍歷屬性 for(Attribute attr : attrList){ //獲取屬性名 String attrName = attr.getName(); //獲取屬性值 String attrValue = attr.getValue(); System.out.print("屬性名:" + ); } System.out.println("======結束解析第" + personList.indexOf(person)+1 + "個人======"); }查看全部
-
1.創建一個SAXBuilder對象 SAXBuilder saxbuilder=newSAXBuilder(); 2.創建輸入流,將xml文件加載到輸入流中(拋出FileNotFoundException) Inputstream in=new FileInputstream("xxx.xml"); 3.通過SAXBuilder的Build方法將輸入流加載到saxb中獲取dom對象 Document doc = saxbuilder.build(in); 4.通過document對象獲取xml文件的根結點 Element rootElement =doc.getRootElement(); 5.獲取根結點下的子節點的List集合查看全部
-
解析xml文件 - 在Java程序中讀取xml文件的過程 解析目的 - 獲取節點名,節點值,屬性名,屬性值 Dom和SAX解析 - java官方提供的解析方式,不需要額外jar包 Dom4j和Jdom需要導入額外的jar包查看全部
-
SAX解析XML的速度比DOM的塊, SAX的解析XML的解析器,需要重寫startElement()開始解析的方法and endElemaent()方法 結束解析的方法and characters()方法 重寫charaters()方法時,String(byte[] bytes,int offset,int length)的構造方法進行數組的傳遞 再去除解析時多余空格 if(!value.trim().equals("")){ System.out.println(value); } 使用 SAX 解析 XML 文件的節點名和節點間文本 startElement方法——String qName(第三個參數):節點名 startElement方法——Attributes attributes(第四個參數):節點名的屬性操作 characters方法——char[] ch(第一個參數):xml整個文本內容,所以需截取想要的內容 如圖代碼+以下代碼 public void endElement(String uri, String localName, String qName) throws SAXException { //調用DefaultHandler類的endElement方法 super.endElement(uri, localName, qName); //判斷是否針對一本書已經遍歷結束 if (qName.equals("book")) { System.out.println("======================結束遍歷某一本書的內容================="); } } public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch, start, length); value = new String(ch, start, length); if (!value.trim().equals("")) { System.out.println("節點值是:" + value); } }查看全部
-
//開始解析book元素的屬性 if(qName.equals("book")) { //已知booke元素下屬性的名稱,根據屬性名稱獲取屬性值 String value = attributes.getValue("id"); System.out.println("book的屬性值是:"+value); //不知道book元素下屬性的名稱以及個數,如何獲取屬性名及屬性值 int num = attributes.getLength(); for(int i = 0; i < num; i++) { System.out.print("book元素的第"+(i+1)+"個屬性是:"+attributes.getQName(i)); System.out.println("---屬性值是:"+attributes.getValue(i)); } }查看全部
-
截圖~查看全部
-
解析步驟查看全部
舉報
0/150
提交
取消