Java 解析 XML

简介: Java 解析 XML标签: Java基础XML解析技术有两种 DOM SAXDOM方式 根据XML的层级结构在内存中分配一个树形结构,把XML的标签,属性和文本等元素都封...

Java 解析 XML

标签: Java基础


XML解析技术有两种 DOM SAX

  • DOM方式
    根据XML的层级结构在内存中分配一个树形结构,把XML的标签,属性和文本等元素都封装成树的节点对象
    • 优点: 便于实现
    • 缺点: XML文件过大可能造成内存溢出
  • SAX方式
    采用事件驱动模型边读边解析:从上到下一行行解析,解析到某一元素, 调用相应解析方法
    • 优点: 不会造成内存溢出,
    • 缺点: 查询不方便,但不能实现

不同的公司和组织提供了针对DOM和SAX两种方式的解析器


JAXP 解析

JAXP是JavaSE的一部分,在javax.xml.parsers包下,分别针对dom与sax提供了如下解析器:

  • Dom
    • DocumentBuilder
    • DocumentBuilderFactory
  • SAX
    • SAXParser
    • SAXParserFactory

示例XML如下,下面我们会使用JAXP对他进行 操作

  • config.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans SYSTEM "constraint.dtd">
<beans>
    <bean id="id1" class="com.fq.domain.Bean">
        <property name="isUsed" value="true"/>
    </bean>
    <bean id="id2" class="com.fq.domain.ComplexBean">
        <property name="refBean" ref="id1"/>
    </bean>
</beans>
  • constraint.dtd
<!ELEMENT beans (bean*) >
        <!ELEMENT bean (property*)>
        <!ATTLIST bean
                id CDATA #REQUIRED
                class CDATA #REQUIRED
                >

        <!ELEMENT property EMPTY>
        <!ATTLIST property
                name CDATA #REQUIRED
                value CDATA #IMPLIED
                ref CDATA #IMPLIED>

JAXP-Dom

/**
 * @author jifang
 * @since 16/1/13下午11:24.
 */
public class XmlRead {

    @Test
    public void client() throws ParserConfigurationException, IOException, SAXException {
        // 生成一个Dom解析器
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        // 解析XML文件
        Document document = builder.parse(ClassLoader.getSystemResourceAsStream("config.xml"));

        // ...
    }
}

DocumentBuilderparse(String/File/InputSource/InputStream param)方法可以将一个XML文件解析为一个Document对象,代表整个文档.
Document(org.w3c.dom包下)是一个接口,其父接口为Node, Node的其他子接口还有Element Attr Text等.

  • Node
Node常用方法 释义
Node appendChild(Node newChild) Adds the node newChild to the end of the list of children of this node.
Node removeChild(Node oldChild) Removes the child node indicated by oldChild from the list of children, and returns it.
NodeList getChildNodes() A NodeList that contains all children of this node.
NamedNodeMap getAttributes() A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.
String getTextContent() This attribute returns the text content of this node and its descendants.
  • Document
Document常用方法 释义
NodeList getElementsByTagName(String tagname) Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.
Element createElement(String tagName) Creates an element of the type specified.
Text createTextNode(String data) Creates a Text node given the specified string.
Attr createAttribute(String name) Creates an Attr of the given name.

Dom查询

  • 解析<bean/>标签上的所有属性
public class XmlRead {

    private Document document;

    @Before
    public void setUp() throws ParserConfigurationException, IOException, SAXException {
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(ClassLoader.getSystemResourceAsStream("config.xml"));
    }

    @Test
    public void client() throws ParserConfigurationException, IOException, SAXException {
        NodeList beans = document.getElementsByTagName("bean");
        for (int i = 0; i < beans.getLength(); ++i) {
            NamedNodeMap attributes = beans.item(i).getAttributes();
            scanNameNodeMap(attributes);
        }
    }

    private void scanNameNodeMap(NamedNodeMap attributes) {
        for (int i = 0; i < attributes.getLength(); ++i) {
            Attr attribute = (Attr) attributes.item(i);
            System.out.printf("%s -> %s%n", attribute.getName(), attribute.getValue());
            // System.out.println(attribute.getNodeName() + " -> " + attribute.getTextContent());
        }
    }
}
  • 打印XML文件所有标签名
@Test
public void client() {
    list(document, 0);
}

private void list(Node node, int depth) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        for (int i = 0; i < depth; ++i)
            System.out.print("\t");
        System.out.println("<" + node.getNodeName() + ">");
    }

    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); ++i) {
        list(childNodes.item(i), depth + 1);
    }
}

Dom添加节点

  • 在第一个<bean/>标签下添加一个<property/>标签,最终结果形式:
<bean id="id1" class="com.fq.domain.Bean">
    <property name="isUsed" value="true"/>
    <property name="name" value="simple-bean">新添加的</property>
</bean>
/**
 * @author jifang
 * @since 16/1/17 下午5:56.
 */
public class XmlAppend {

    // 文档回写器
    private Transformer transformer;

    // xml文档
    private Document document;

    @Before
    public void setUp() throws ParserConfigurationException, IOException, SAXException {
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(ClassLoader.getSystemResourceAsStream("config.xml"));
    }

    @Test
    public void client() {
        // 得到第一bean标签
        Node firstBean = document.getElementsByTagName("bean").item(0);

        /** 创建一个property标签 **/
        Element property = document.createElement("property");

        // 为property标签添加属性
        // property.setAttribute("name", "name");
        // property.setAttribute("value", "feiqing");
        Attr name = document.createAttribute("name");
        name.setValue("name");
        property.setAttributeNode(name);
        Attr value = document.createAttribute("value");
        value.setValue("simple-bean");
        property.setAttributeNode(value);

        // 为property标签添加内容
        //property.setTextContent("新添加的");
        property.appendChild(document.createTextNode("新添加的"));

        // 将property标签添加到bean标签下
        firstBean.appendChild(property);
    }

    @After
    public void tearDown() throws TransformerException {
        transformer = TransformerFactory.newInstance().newTransformer();

        // 写回XML
        transformer.transform(new DOMSource(document),
                new StreamResult("src/main/resources/config.xml"));
    }
}

注意: 必须将内存中的DOM写回XML文档才能生效


Dom更新节点

  • 将刚刚添加的<property/>修改如下
<property name="name" value="new-simple-bean">simple-bean是新添加的</property>
@Test
public void client() {
    NodeList properties = document.getElementsByTagName("property");
    for (int i = 0; i < properties.getLength(); ++i) {
        Element property = (Element) properties.item(i);
        if (property.getAttribute("value").equals("simple-bean")) {
            property.setAttribute("value", "new-simple-bean");
            property.setTextContent("simple-bean是新添加的");
            break;
        }
    }
}

Dom删除节点

删除刚刚修改的<property/>标签

@Test
public void client() {
    NodeList properties = document.getElementsByTagName("property");
    for (int i = 0; i < properties.getLength(); ++i) {
        Element property = (Element) properties.item(i);
        if (property.getAttribute("value").equals("new-simple-bean")) {
            property.getParentNode().removeChild(property);
            break;
        }
    }
}

JAXP-SAX

SAXParser实例需要从SAXParserFactory实例的newSAXParser()方法获得, 用于解析XML文件的parse(String uri, DefaultHandler dh)方法没有返回值,但比DOM方法多了一个事件处理器参数DefaultHandler:

  • 解析到开始标签,自动调用DefaultHandlerstartElement()方法;
  • 解析到标签内容(文本),自动调用DefaultHandlercharacters()方法;
  • 解析到结束标签,自动调用DefaultHandlerendElement()方法.

Sax查询

  • 打印整个XML文档
/**
 * @author jifang
 * @since 16/1/17 下午9:16.
 */
public class SaxRead {

    @Test
    public void client() throws ParserConfigurationException, IOException, SAXException {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        parser.parse(ClassLoader.getSystemResourceAsStream("config.xml"), new SaxHandler());
    }

    private class SaxHandler extends DefaultHandler {

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            System.out.print("<" + qName);
            for (int i = 0; i < attributes.getLength(); ++i) {
                String attrName = attributes.getQName(i);
                String attrValue = attributes.getValue(i);
                System.out.print(" " + attrName + "=" + attrValue);
            }
            System.out.print(">");
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            System.out.print(new String(ch, start, length));
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            System.out.print("</" + qName + ">");
        }
    }
}
  • 打印所有property标签内容的Handler
private class SaxHandler extends DefaultHandler {
    // 用互斥锁保护isProperty变量
    private boolean isProperty = false;
    private Lock mutex = new ReentrantLock();

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equals("property")) {
            mutex.lock();
            isProperty = true;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        // 只有被锁定之后才有可能是true
        if (isProperty) {
            System.out.println(new String(ch, start, length));
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equals("property")) {
            try {
                isProperty = false;
            } finally {
                mutex.unlock();
            }
        }
    }
}

注: SAX方式不能实现 操作.


Dom4j解析

Dom4j是JDom的一种智能分支,从原先的JDom组织中分离出来,提供了比JDom功能更加强大,性能更加卓越的Dom4j解析器(比如提供对XPath支持).
使用Dom4j需要在pom中添加如下依赖:

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

示例XML如下,下面我们会使用Dom4j对他进行 操作:

  • config.xml
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.fq.me/context"
       xsi:schemaLocation="http://www.fq.me/context http://www.fq.me/context/context.xsd">
    <bean id="id1" class="com.fq.benz">
        <property name="name" value="benz"/>
    </bean>
    <bean id="id2" class="com.fq.domain.Bean">
        <property name="isUsed" value="true"/>
        <property name="complexBean" ref="id1"/>
    </bean>
</beans>
  • context.xsd
<?xml version="1.0" encoding="utf-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.fq.me/context"
        elementFormDefault="qualified">
    <element name="beans">
        <complexType>
            <sequence>
                <element name="bean" maxOccurs="unbounded">
                    <complexType>
                        <sequence>
                            <element name="property" maxOccurs="unbounded">
                                <complexType>
                                    <attribute name="name" type="string" use="required"/>
                                    <attribute name="value" type="string" use="optional"/>
                                    <attribute name="ref" type="string" use="optional"/>
                                </complexType>
                            </element>
                        </sequence>
                        <attribute name="id" type="string" use="required"/>
                        <attribute name="class" type="string" use="required"/>
                    </complexType>
                </element>
            </sequence>
        </complexType>
    </element>
</schema>
/**
 * @author jifang
 * @since 16/1/18下午4:02.
 */
public class Dom4jRead {

    @Test
    public void client() throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(ClassLoader.getSystemResource("config.xml"));
        // ...
    }
}

与JAXP类似Document也是一个接口(org.dom4j包下),其父接口是Node, Node的子接口还有Element Attribute Document Text CDATA Branch

  • Node
Node常用方法 释义
Element getParent() getParent returns the parent Element if this node supports the parent relationship or null if it is the root element or does not support the parent relationship.
  • Document
Document常用方法 释义
Element getRootElement() Returns the root Elementfor this document.
  • Element
Element常用方法 释义
void add(Attribute/Text param) Adds the given Attribute/Text to this element.
Element addAttribute(String name, String value) Adds the attribute value of the given local name.
Attribute attribute(int index) Returns the attribute at the specified indexGets the
Attribute attribute(String name) Returns the attribute with the given name
Element element(String name) Returns the first element for the given local name and any namespace.
Iterator elementIterator() Returns an iterator over all this elements child elements.
Iterator elementIterator(String name) Returns an iterator over the elements contained in this element which match the given local name and any namespace.
List elements() Returns the elements contained in this element.
List elements(String name) Returns the elements contained in this element with the given local name and any namespace.
  • Branch
Branch常用方法 释义
Element addElement(String name) Adds a new Element node with the given name to this branch and returns a reference to the new node.
boolean remove(Node node) Removes the given Node if the node is an immediate child of this branch.

Dom4j查询

  • 打印所有属性信息:
/**
 * @author jifang
 * @since 16/1/18下午4:02.
 */
public class Dom4jRead {

    private Document document;

    @Before
    public void setUp() throws DocumentException {
        document = new SAXReader()
                .read(ClassLoader.getSystemResource("config.xml"));
    }

    @Test
    @SuppressWarnings("unchecked")
    public void client() {
        Element beans = document.getRootElement();

        for (Iterator iterator = beans.elementIterator(); iterator.hasNext(); ) {
            Element bean = (Element) iterator.next();
            String id = bean.attributeValue("id");
            String clazz = bean.attributeValue("class");
            System.out.println("id: " + id + ", class: " + clazz);

            scanProperties(bean.elements());
        }
    }

    public void scanProperties(List<? extends Element> properties) {
        for (Element property : properties) {
            System.out.print("name: " + property.attributeValue("name"));
            Attribute value = property.attribute("value");
            if (value != null) {
                System.out.println("," + value.getName() + ": " + value.getValue());
            }
            Attribute ref = property.attribute("ref");
            if (ref != null) {
                System.out.println("," + ref.getName() + ": " + ref.getValue());
            }
        }
    }
}

Dom4j添加节点

在第一个<bean/>标签末尾添加<property/>标签

<bean id="id1" class="com.fq.benz"> 
    <property name="name" value="benz"/>  
    <property name="refBean" ref="id2">新添加的标签</property>
</bean>  
/**
 * @author jifang
 * @since 16/1/19上午9:50.
 */
public class Dom4jAppend {

    //...

    @Test
    public void client() {
        Element beans = document.getRootElement();
        Element firstBean = beans.element("bean");
        Element property = firstBean.addElement("property");
        property.addAttribute("name", "refBean");
        property.addAttribute("ref", "id2");
        property.setText("新添加的标签");
    }

    @After
    public void tearDown() throws IOException {
        // 回写XML
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileOutputStream("src/main/resources/config.xml"), format);
        writer.write(document);
    }
}

我们可以将获取读写XML操作封装成一个工具, 以后调用时会方便些:

/**
 * @author jifang
 * @since 16/1/19下午2:12.
 */
public class XmlUtils {

    public static Document getXmlDocument(String config) {
        try {
            return new SAXReader().read(ClassLoader.getSystemResource(config));
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }

    public static void writeXmlDocument(String path, Document document) {
        try {
            new XMLWriter(new FileOutputStream(path), OutputFormat.createPrettyPrint()).write(document);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
  • 在第一个<bean/>的第一个<property/>后面添加一个<property/>标签
<bean id="id1" class="com.fq.benz"> 
    <property name="name" value="benz"/>  
    <property name="rate" value="3.14"/>
    <property name="refBean" ref="id2">新添加的标签</property> 
</bean>  
public class Dom4jAppend {

    private Document document;

    @Before
    public void setUp() {
        document = XmlUtils.getXmlDocument("config.xml");
    }

    @Test
    @SuppressWarnings("unchecked")
    public void client() {
        Element beans = document.getRootElement();
        Element firstBean = beans.element("bean");
        List<Element> properties = firstBean.elements();

        //Element property = DocumentHelper
        // .createElement(QName.get("property", firstBean.getNamespaceURI()));
        Element property = DocumentFactory.getInstance()
                .createElement("property", firstBean.getNamespaceURI());
        property.addAttribute("name", "rate");
        property.addAttribute("value", "3.14");
        properties.add(1, property);
    }

    @After
    public void tearDown() {
        XmlUtils.writeXmlDocument("src/main/resources/config.xml", document);
    }
}

Dom4j修改节点

  • id1 bean的第一个<property/>修改如下:
<property name="name" value="翡青"/>  
@Test
@SuppressWarnings("unchecked")
public void client() {
    Element beans = document.getRootElement();
    Element firstBean = beans.element("bean");
    List<Element> properties = firstBean.elements();

    Element property = DocumentFactory.getInstance()
            .createElement("property", firstBean.getNamespaceURI());
    property.addAttribute("name", "rate");
    property.addAttribute("value", "3.14");
    properties.add(1, property);
}

Dom4j 删除节点

  • 删除刚刚修改的节点
@Test
@SuppressWarnings("unchecked")
public void delete() {
    List<Element> beans = document.getRootElement().elements("bean");
    for (Element bean : beans) {
        if (bean.attributeValue("id").equals("id1")) {
            List<Element> properties = bean.elements("property");
            for (Element property : properties) {
                if (property.attributeValue("name").equals("name")) {
                    // 执行删除动作
                    property.getParent().remove(property);
                    break;
                }
            }
            break;
        }
    }
}

Dom4j实例

Java 反射一文中我们实现了根据JSON配置文件来加载bean的对象池,现在我们可以为其添加根据XML配置(XML文件同前):

/**
 * @author jifang
 * @since 16/1/18下午9:18.
 */
public class XmlParse {

    private static final ObjectPool POOL = ObjectPoolBuilder.init(null);

    public static Element parseBeans(String config) {
        try {
            return new SAXReader().read(ClassLoader.getSystemResource(config)).getRootElement();
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }

    public static void processObject(Element bean, List<? extends Element> properties)
            throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        Class<?> clazz = Class.forName(bean.attributeValue(CommonConstant.CLASS));
        Object targetObject = clazz.newInstance();

        for (Element property : properties) {
            String fieldName = property.attributeValue(CommonConstant.NAME);
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            // 含有value属性
            if (property.attributeValue(CommonConstant.VALUE) != null) {
                SimpleValueSetUtils.setSimpleValue(field, targetObject, property.attributeValue(CommonConstant.VALUE));
            } else if (property.attributeValue(CommonConstant.REF) != null) {
                String refId = property.attributeValue(CommonConstant.REF);
                Object object = POOL.getObject(refId);
                field.set(targetObject, object);
            } else {
                throw new RuntimeException("neither value nor ref");
            }
        }

        POOL.putObject(bean.attributeValue(CommonConstant.ID), targetObject);
    }
}

注: 上面代码只是对象池项目的XML解析部分,完整项目可参考git@git.oschina.net:feiqing/commons-frame.git


XPath

XPath是一门在XML文档中查找信息的语言,XPath可用来在XML文档中对元素和属性进行遍历.

表达式 描述
/ 从根节点开始获取(/beans:匹配根下的<beans/>; /beans/bean:匹配<beans/>下面的<bean/>)
// 从当前文档中搜索,而不用考虑它们的位置(//property: 匹配当前文档中所有<property/>)
* 匹配任何元素节点(/*: 匹配所有标签)
@ 匹配属性(例: //@name: 匹配所有name属性)
[position] 位置谓语匹配(例: //property[1]: 匹配第一个<property/>;//property[last()]: 匹配最后一个<property/>)
[@attr] 属性谓语匹配(例: //bean[@id]: 匹配所有带id属性的标签; //bean[@id='id1']: 匹配所有id属性值为’id1’的标签)

谓语: 谓语用来查找某个特定的节点或者包含某个指定的值的节点.

XPath的语法详细内容可以参考W3School XPath 教程.


Dom4j对XPath的支持

默认的情况下Dom4j并不支持XPath, 需要在pom下添加如下依赖:

<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
    <version>1.1.6</version>
</dependency>

Dom4jNode接口提供了方法对XPath支持:

方法
List selectNodes(String xpathExpression)
List selectNodes(String xpathExpression, String comparisonXPathExpression)
List selectNodes(String xpathExpression, String comparisonXPathExpression, boolean removeDuplicates)
Object selectObject(String xpathExpression)
Node selectSingleNode(String xpathExpression)

XPath实现查询

  • 查询所有bean标签上的属性值
/**
 * @author jifang
 * @since 16/1/20上午9:28.
 */
public class XPathRead {

    private Document document;

    @Before
    public void setUp() throws DocumentException {
        document = XmlUtils.getXmlDocument("config.xml");
    }

    @Test
    @SuppressWarnings("unchecked")
    public void client() {
        List<Element> beans = document.selectNodes("//bean");
        for (Element bean : beans) {
            System.out.println("id: " + bean.attributeValue("id") +
                    ", class: " + bean.attributeValue("class"));
        }
    }
}

XPath实现更新

  • 删除id=”id2”的<bean/>
@Test
public void client() {
    Node bean = document.selectSingleNode("//bean[@id=\"id2\"]");
    bean.getParent().remove(bean);
}

参考:
Dom4j的使用
Java 处理 XML 的三种主流技术及介绍
目录
相关文章
|
3月前
|
存储 Java 文件存储
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
252 1
|
19天前
|
人工智能 前端开发 Java
Java 面试资料中相关代码使用方法与组件封装方法解析
这是一份详尽的Java面试资料代码指南,涵盖使用方法与组件封装技巧。内容包括环境准备(JDK 8+、Maven/Gradle)、核心类示例(问题管理、学习进度跟踪)、Web应用部署(Spring Boot、前端框架)、单元测试及API封装。通过问题库管理、数据访问组件、学习进度服务和REST接口等模块化设计,帮助开发者高效组织与复用功能,同时支持扩展如用户认证、AI推荐等功能。适用于Java核心技术学习与面试备考,提升编程与设计能力。资源链接:[点此下载](https://pan.quark.cn/s/14fcf913bae6)。
50 6
Java 面试资料中相关代码使用方法与组件封装方法解析
|
14天前
|
搜索推荐 算法 Java
2025 年互联网大厂校园招聘 JAVA 工程师笔试题及备考要点解析
本文针对互联网大厂校招Java工程师笔试题进行解析,涵盖基础知识、面向对象编程、数据结构与算法、异常处理及集合框架等核心内容。从数据类型、运算符到流程控制语句,从类与对象、继承多态到数组链表、排序算法,再到异常捕获与集合框架应用,结合实际案例深入剖析,助你系统掌握考点,提升应试能力。资源链接:[点此获取](https://pan.quark.cn/s/14fcf913bae6)。
38 9
|
13天前
|
SQL Java 数据库连接
java 校招需要准备哪些内容及关键要点解析
这是一篇针对Java校招准备的详细指南,涵盖六大核心板块:扎实的Java基础知识(如数据类型、面向对象编程、集合框架)、数据库相关知识(SQL操作与管理工具)、Java开发框架(Spring、Spring Boot、MyBatis)、其他重要知识(多线程编程、网络编程、数据结构与算法)、项目经验准备以及面试技巧。文章结合技术方案与应用实例,帮助应届生全面掌握校招所需技能,从理论到实践全面提升竞争力。资源地址:[https://pan.quark.cn/s/14fcf913bae6](https://pan.quark.cn/s/14fcf913bae6)。
35 1
|
14天前
|
算法 Java 关系型数据库
校招 Java 面试基础题目解析及学习指南含新技术实操要点
本指南聚焦校招Java面试,涵盖Java 8+新特性、多线程与并发、集合与泛型改进及实操项目。内容包括Lambda表达式、Stream API、Optional类、CompletableFuture异步编程、ReentrantLock与Condition、局部变量类型推断(var)、文本块、模块化系统等。通过在线书店系统项目,实践Java核心技术,如书籍管理、用户管理和订单管理,结合Lambda、Stream、CompletableFuture等特性。附带资源链接,助你掌握最新技术,应对面试挑战。
33 2
|
15天前
|
缓存 NoSQL Java
校招 Java 面试常见知识点及实战案例全解析
本文全面解析了Java校招面试中的常见知识点,涵盖Java新特性(如Lambda表达式、、Optional类)、集合框架高级应用(线程安全集合、Map性能优化)、多线程与并发编程(线程池配置)、JVM性能调优(内存溢出排查、垃圾回收器选择)、Spring与微服务实战(Spring Boot自动配置)、数据库与ORM框架(MyBatis高级用法、索引优化)、分布式系统(分布式事务、缓存应用)、性能优化(接口优化、高并发限流)、单元测试与代码质量(JUnit 5、Mockito、JaCoCo)以及项目实战案例(电商秒杀系统、社交消息推送)。资源地址: [https://pan.quark.cn/s
66 4
|
14天前
|
SQL Java 数据库连接
阿里腾讯互联网公司校招 Java 面试题总结及答案解析
本文总结了阿里巴巴和腾讯等互联网大厂的Java校招面试题及答案,涵盖Java基础、多线程、集合框架、数据库、Spring与MyBatis框架等内容。从数据类型、面向对象特性到异常处理,从线程安全到SQL优化,再到IOC原理与MyBatis结果封装,全面梳理常见考点。通过详细解析,帮助求职者系统掌握Java核心知识,为校招做好充分准备。资源链接:[点击下载](https://pan.quark.cn/s/14fcf913bae6)。
29 2
|
14天前
|
Java 数据库连接 API
互联网大厂校招 JAVA 工程师笔试题解析及常见考点分析
本文深入解析互联网大厂校招Java工程师笔试题,涵盖基础知识(数据类型、流程控制)、面向对象编程(类与对象、继承与多态)、数据结构与算法(数组、链表、排序算法)、异常处理、集合框架、Java 8+新特性(Lambda表达式、Stream API)、多线程与并发、IO与NIO、数据库操作(JDBC、ORM框架MyBatis)及Spring框架基础(IoC、DI、AOP)。通过技术方案讲解与实例演示,助你掌握核心考点,提升解题能力。
54 2
|
15天前
|
Java 关系型数据库 MySQL
2025 年互联网公司校招 Java 面试题总结及答案实操示例解析
本项目基于Spring Boot 3与Java 17技术栈,围绕校园招聘常见面试题,提供核心知识点的实操示例。涵盖多线程、RESTful API设计、数据库操作(Spring Data JPA)、事务管理及异常处理等。通过完整代码实现与运行步骤,帮助理解用户管理、线程池配置等实际应用场景。资源包含项目结构、关键代码示例(如User实体类、UserService服务层、ThreadService多线程实现)及数据库迁移脚本,适合深入学习与实践。环境要求:JDK 17+、Maven 3.8+、MySQL 8.0+。
64 3
|
15天前
|
存储 安全 算法
Java 集合面试题 PDF 下载及高频考点解析
本文围绕Java集合面试题展开,详细解析了集合框架的基本概念、常见集合类的特点与应用场景。内容涵盖`ArrayList`与`LinkedList`的区别、`HashSet`与`TreeSet`的对比、`HashMap`与`ConcurrentHashMap`的线程安全性分析等。通过技术方案与应用实例,帮助读者深入理解集合类的特性和使用场景,提升解决实际开发问题的能力。文末附带资源链接,供进一步学习参考。
28 4

推荐镜像

更多
  • DNS