📜  Apache Xerces教程(1)

📅  最后修改于: 2023-12-03 14:39:17.593000             🧑  作者: Mango

Apache Xerces教程

Apache Xerces是一个开源的XML解析器,用于解析和验证XML文档。它是Java和C++版本的,本教程将介绍Java版本。

安装

你可以从Apache Xerces的官方网站下载Java版本的安装包。下载完成后,将jar包添加到项目的classpath中。

解析XML文档

要解析XML文档,需要创建DocumentBuilderFactory和DocumentBuilder对象。下面是示例代码:

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;

public class XMLParser {
   public static void main(String[] args){

      try {
         File inputFile = new File("input.xml");
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(inputFile);
         doc.getDocumentElement().normalize();
         System.out.println("根元素:" + doc.getDocumentElement().getNodeName());
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
遍历XML节点

使用getFirstChild()和getNextSibling()方法遍历XML节点。以下代码示例遍历了所有的student节点:

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class XMLParser {
   public static void main(String[] args){

      try {
         File inputFile = new File("input.xml");
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(inputFile);
         doc.getDocumentElement().normalize();
         System.out.println("根元素:" + doc.getDocumentElement().getNodeName());
         NodeList nList = doc.getElementsByTagName("student");
         System.out.println("----------------------------");

         for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            System.out.println("\n当前元素 :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
              Element eElement = (Element) nNode;
              System.out.println("学号 : " 
                 + eElement.getAttribute("id"));
              System.out.println("姓名 : " 
                 + eElement
                 .getElementsByTagName("name")
                 .item(0)
                 .getTextContent());
              System.out.println("年龄 : " 
                 + eElement
                 .getElementsByTagName("age")
                 .item(0)
                 .getTextContent());
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
验证XML文档

要验证XML文档,可以使用DocumentBuilderFactory的setSchema()方法来设置一个XML Schema文件。然后,可以调用Document的validate()方法来验证文档。以下代码示例展示了如何验证XML文档:

import java.io.File;
import javax.xml.XMLConstants; 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException; 

public class XMLValidator {
   public static void main(String[] args){
      String xmlPath = "input.xml";
      String xsdPath = "schema.xsd";

      try {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setNamespaceAware(true);
         SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
         Schema schema = schemaFactory.newSchema(new File(xsdPath));
         Validator validator = schema.newValidator();
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document document = builder.parse(new File(xmlPath));
         DOMSource source = new DOMSource(document);
         validator.validate(source);
         System.out.println(xmlPath + " is valid."); 
      } catch (ParserConfigurationException | SAXException | IOException e) {
         System.out.println(xmlPath + " is not valid because ");
         System.out.println(e.getMessage());
      }
   }
}

以上就是Apache Xerces的基本用法,希望能够帮助到你。