Java : Entity (XML) con ejemplos

Entity (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de Entity.

Nota :


Summary

Esta interfaz representa una entidad conocida, ya sea analizada o no, en un documento XML. Tenga en cuenta que esto modela la entidad en sí, no la declaración de entidad. (Traducción automática)

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

Consulte el siguiente enlace.

Methods

String getInputEncoding ()

Un atributo que especifica la codificación utilizada para esta entidad en el momento del análisis, cuando es una entidad analizada externa. (Traducción automática)

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 ()

Para entidades no analizadas, el nombre de la notación de la entidad. (Traducción automática)

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 ()

El identificador público asociado con la entidad si se especifica, y nulo en caso contrario. (Traducción automática)

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 ()

El identificador del sistema asociado con la entidad si se especifica, y es nulo en caso contrario. (Traducción automática)

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 ()

Un atributo que especifica, como parte de la declaración de texto, la codificación de esta entidad, cuando es una entidad analizada externa. (Traducción automática)

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 ()

Un atributo que especifica, como parte de la declaración de texto, el número de versión de esta entidad, cuando es una entidad analizada externa. (Traducción automática)

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

Consulte el siguiente enlace.


Related posts

To top of page