Java : Entity (XML) 示例

Entity (Java SE 22 & JDK 22) 示例。
您将在大多数 Entity 方法中找到代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

此接口表示 XML 文档中已知实体(已解析或未解析)。请注意,此接口模拟的是实体本身,而不是实体声明。 (机器翻译)

Class diagram

Code examples on this page use the printDocument method below.

public void printDocument(Document document) throws TransformerException {
    final var transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    final var result = new StreamResult(new StringWriter());
    transformer.transform(new DOMSource(document), result);

    System.out.println(result.getWriter());
}
final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx

Files.writeString(path, "yyy");

final var xml = """
        <!DOCTYPE root [
            <!ENTITY aaa "bbb">
            <!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
        ]>
        <root>
          &aaa;
          &xxx;
        </root>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

//<root>
//  bbb
//  yyy
//</root>
printDocument(document);

final var entities = document.getDoctype().getEntities();

if (entities.getNamedItem("aaa") instanceof Entity entity) {
    System.out.println(entity); // [aaa: null]
    System.out.println(entity.getSystemId()); // null
}

if (entities.getNamedItem("xxx") instanceof Entity entity) {
    System.out.println(entity); // [xxx: null]
    System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
}

Fields declared in Node

ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE

请参阅下面的链接。

Methods

String getInputEncoding ()

当实体为外部解析实体时,该属性指定解析时对此实体使用的编码。 (机器翻译)

final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx

Files.writeString(path, "yyy");

final var xml = """
        <!DOCTYPE root [
            <!ENTITY aaa "bbb">
            <!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
        ]>
        <root>
          &aaa;
          &xxx;
        </root>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

//<root>
//  bbb
//  yyy
//</root>
printDocument(document);

final var entities = document.getDoctype().getEntities();

if (entities.getNamedItem("aaa") instanceof Entity entity) {
    System.out.println(entity); // [aaa: null]
    System.out.println(entity.getInputEncoding()); // null
    System.out.println(entity.getSystemId()); // null
}

if (entities.getNamedItem("xxx") instanceof Entity entity) {
    System.out.println(entity); // [xxx: null]
    System.out.println(entity.getInputEncoding()); // UTF-8
    System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
}

String getNotationName ()

对于未解析的实体,实体符号的名称。 (机器翻译)

final var xml = """
        <!DOCTYPE root [
            <!ENTITY aaa SYSTEM "bbb" NDATA ccc>
        ]>
        <root/>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var entities = document.getDoctype().getEntities();

if (entities.getNamedItem("aaa") instanceof Entity entity) {
    System.out.println(entity); // [aaa: null]
    System.out.println(entity.getSystemId()); // bbb
    System.out.println(entity.getNotationName()); // ccc
}

String getPublicId ()

如果指定,则为与实体关联的公共标识符,否则为 null。 (机器翻译)

final var xml = """
        <!DOCTYPE root [
            <!ENTITY aaa PUBLIC "bbb" "ccc">
        ]>
        <root/>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

final var entities = document.getDoctype().getEntities();

if (entities.getNamedItem("aaa") instanceof Entity entity) {
    System.out.println(entity); // [aaa: null]
    System.out.println(entity.getPublicId()); // bbb
    System.out.println(entity.getSystemId()); // ccc
}

String getSystemId ()

如果指定,则为与实体关联的系统标识符,否则为空。 (机器翻译)

final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx

Files.writeString(path, "yyy");

final var xml = """
        <!DOCTYPE root [
            <!ENTITY aaa "bbb">
            <!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
        ]>
        <root>
          &aaa;
          &xxx;
        </root>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

//<root>
//  bbb
//  yyy
//</root>
printDocument(document);

final var entities = document.getDoctype().getEntities();

if (entities.getNamedItem("aaa") instanceof Entity entity) {
    System.out.println(entity); // [aaa: null]
    System.out.println(entity.getInputEncoding()); // null
    System.out.println(entity.getSystemId()); // null
}

if (entities.getNamedItem("xxx") instanceof Entity entity) {
    System.out.println(entity); // [xxx: null]
    System.out.println(entity.getInputEncoding()); // UTF-8
    System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
}

String getXmlEncoding ()

作为文本声明的一部分,当实体是外部解析实体时,指定该实体的编码的属性。 (机器翻译)

final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx

Files.writeString(path, """
        <?xml version="1.0" encoding="UTF-8"?>\
        yyy\
        """);

final var xml = """
        <!DOCTYPE root [
            <!ENTITY aaa "bbb">
            <!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
        ]>
        <root>
          &aaa;
          &xxx;
        </root>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

//<root>
//  bbb
//  yyy
//</root>
printDocument(document);

final var entities = document.getDoctype().getEntities();

if (entities.getNamedItem("aaa") instanceof Entity entity) {
    System.out.println(entity); // [aaa: null]
    System.out.println(entity.getSystemId()); // null
    System.out.println(entity.getXmlVersion()); // null
    System.out.println(entity.getXmlEncoding()); // null
}

if (entities.getNamedItem("xxx") instanceof Entity entity) {
    System.out.println(entity); // [xxx: null]
    System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
    System.out.println(entity.getXmlVersion()); // 1.0
    System.out.println(entity.getXmlEncoding()); // UTF-8
}
final var path = Path.of("R:", "java-work", "aaa");
System.out.println(path); // R:\java-work\aaa

Files.writeString(path, """
        <?xml version="1.1" encoding="UTF-16"?>\
        bbb\
        """, StandardCharsets.UTF_16);

final var xml = """
        <?xml version="1.1" encoding="UTF-8"?>
        <!DOCTYPE root [
            <!ENTITY aaa SYSTEM "file:///R:/java-work/aaa">
        ]>
        <root>
          &aaa;
        </root>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

//<root>
//  bbb
//</root>
printDocument(document);

final var entities = document.getDoctype().getEntities();

if (entities.getNamedItem("aaa") instanceof Entity entity) {
    System.out.println(entity); // [aaa: null]
    System.out.println(entity.getSystemId()); // file:///R:/java-work/aaa
    System.out.println(entity.getXmlVersion()); // 1.1
    System.out.println(entity.getXmlEncoding()); // UTF-16
}

String getXmlVersion ()

作为文本声明的一部分,当实体为外部解析实体时,指定该实体的版本号的属性。 (机器翻译)

final var path = Path.of("R:", "java-work", "xxx");
System.out.println(path); // R:\java-work\xxx

Files.writeString(path, """
        <?xml version="1.0" encoding="UTF-8"?>\
        yyy\
        """);

final var xml = """
        <!DOCTYPE root [
            <!ENTITY aaa "bbb">
            <!ENTITY xxx SYSTEM "file:///R:/java-work/xxx">
        ]>
        <root>
          &aaa;
          &xxx;
        </root>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

//<root>
//  bbb
//  yyy
//</root>
printDocument(document);

final var entities = document.getDoctype().getEntities();

if (entities.getNamedItem("aaa") instanceof Entity entity) {
    System.out.println(entity); // [aaa: null]
    System.out.println(entity.getSystemId()); // null
    System.out.println(entity.getXmlVersion()); // null
    System.out.println(entity.getXmlEncoding()); // null
}

if (entities.getNamedItem("xxx") instanceof Entity entity) {
    System.out.println(entity); // [xxx: null]
    System.out.println(entity.getSystemId()); // file:///R:/java-work/xxx
    System.out.println(entity.getXmlVersion()); // 1.0
    System.out.println(entity.getXmlEncoding()); // UTF-8
}
final var path = Path.of("R:", "java-work", "aaa");
System.out.println(path); // R:\java-work\aaa

Files.writeString(path, """
        <?xml version="1.1" encoding="UTF-16"?>\
        bbb\
        """, StandardCharsets.UTF_16);

final var xml = """
        <?xml version="1.1" encoding="UTF-8"?>
        <!DOCTYPE root [
            <!ENTITY aaa SYSTEM "file:///R:/java-work/aaa">
        ]>
        <root>
          &aaa;
        </root>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();
final var document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

//<root>
//  bbb
//</root>
printDocument(document);

final var entities = document.getDoctype().getEntities();

if (entities.getNamedItem("aaa") instanceof Entity entity) {
    System.out.println(entity); // [aaa: null]
    System.out.println(entity.getSystemId()); // file:///R:/java-work/aaa
    System.out.println(entity.getXmlVersion()); // 1.1
    System.out.println(entity.getXmlEncoding()); // UTF-16
}

Methods declared in Node

appendChild, cloneNode, compareDocumentPosition, getAttributes, getBaseURI, getChildNodes, getFeature, getFirstChild, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeName, getNodeType, getNodeValue, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getTextContent, getUserData, hasAttributes, hasChildNodes, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, removeChild, replaceChild, setNodeValue, setPrefix, setTextContent, setUserData

请参阅下面的链接。


相关文章

To top of page