การทำให้เป็นมาตรฐาน DOM มีผลเหมือนกันโดยไม่ต้องทำให้เป็นมาตรฐาน

อ่านคำตอบที่นี่: การทำให้เป็นมาตรฐานใน DOM parsing ด้วย java - มันทำงานยังไง?

ฉันเข้าใจว่าการทำให้เป็นมาตรฐานจะลบโหนดข้อความว่างที่อยู่ติดกัน ฉันลองใช้ xml ต่อไปนี้:

<company>hello
wor
ld
</company>

ด้วยรหัสต่อไปนี้:

    try {
        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();


        Document doc = dBuilder.parse(file);
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        System.out.println(doc.getDocumentElement().getChildNodes().getLength());
        System.out.println(doc.getDocumentElement().getChildNodes().item(0).getTextContent());

    } catch (Exception e) {
        e.printStackTrace();
    }

ฉันมักจะได้รับโหนดลูก 1 โหนดสำหรับองค์ประกอบ "บริษัท" แม้ว่าจะไม่มีการทำให้เป็นมาตรฐานก็ตาม ผลลัพธ์คือ:

Root element :company
1
hello
wor
ld

แล้วมีอะไรผิดปกติที่นี่? ใครช่วยอธิบายได้ไหม? ฉันไม่ควรรับสวัสดีชาวโลกในบรรทัดเดียว


person Mohammad Karmi    schedule 26.12.2018    source แหล่งที่มา


คำตอบ (1)


โปรแกรมแยกวิเคราะห์กำลังสร้างแผนผัง DOM ที่ทำให้เป็นมาตรฐานแล้ว

เมธอด normalize() มีประโยชน์เมื่อคุณสร้าง/แก้ไข DOM ซึ่งอาจไม่ส่งผลให้เกิดแผนภูมิต้นไม้มาตรฐาน ซึ่งในกรณีนี้ เมธอดจะทำให้ต้นไม้เป็นมาตรฐานสำหรับคุณ

ผู้ช่วยทั่วไป

private static void printDom(String indent, Node node) {
    System.out.println(indent + node);
    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling())
        printDom(indent + "  ", child);
}

ตัวอย่างที่ 1

public static void main(String[] args) throws Exception {
    String xml = "<Root>text 1<!-- test -->text 2</Root>";
    DocumentBuilder domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = domBuilder.parse(new InputSource(new StringReader(xml)));
    printDom("", doc);
    deleteComments(doc);
    printDom("", doc);
    doc.normalizeDocument();
    printDom("", doc);
}
private static void deleteComments(Node node) {
    if (node.getNodeType() == Node.COMMENT_NODE)
        node.getParentNode().removeChild(node);
    else {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++)
            deleteComments(children.item(i));
    }
}

เอาท์พุท

[#document: null]
  [Root: null]
    [#text: text 1]
    [#comment:  test ]
    [#text: text 2]
[#document: null]
  [Root: null]
    [#text: text 1]
    [#text: text 2]
[#document: null]
  [Root: null]
    [#text: text 1text 2]

ตัวอย่างที่ 2

public static void main(String[] args) throws Exception {
    DocumentBuilder domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = domBuilder.newDocument();
    Element root = doc.createElement("Root");
    doc.appendChild(root);
    root.appendChild(doc.createTextNode("Hello"));
    root.appendChild(doc.createTextNode(" "));
    root.appendChild(doc.createTextNode("World"));
    printDom("", doc);
    doc.normalizeDocument();
    printDom("", doc);
}

เอาท์พุท

[#document: null]
  [Root: null]
    [#text: Hello]
    [#text:  ]
    [#text: World]
[#document: null]
  [Root: null]
    [#text: Hello World]
person Andreas    schedule 26.12.2018