Aspose.Wordsรูปแบบวัตถุเอกสาร(DOM)
รูปแบบวัตถุเอกสารAspose.Words(DOM)เป็นตัวแทนในหน่วยความจำของเอกสารคำ Aspose.WordsDOMช่วยให้คุณสามารถอ่านจัดการและปรับเปลี่ยนเนื้อหาและการจัดรูปแบบของเอกสารคำ.
ส่วนนี้อธิบายชั้นเรียนหลักของAspose.WordsDOMและความสัมพันธ์ของชั้นเรียน โดยใช้ชั้นเรียนAspose.WordsDOMคุณสามารถขอรับการเข้าถึงแบบโปรแกรมไปยังองค์ประกอบของเอกสา.
สร้างต้นไม้วัตถุเอกสาร
เมื่ออ่านเอกสารใน Aspose.Words DOM จะมีการสร้างออบเจ็กต์ทรี และองค์ประกอบประเภทต่างๆ ของเอกสารต้นฉบับจะมีออบเจ็กต์ทรี DOM ของตัวเองซึ่งมีคุณสมบัติต่างกัน.
สร้างต้นไม้โหนดเอกสาร
เมื่อAspose.Wordsอ่านเอกสารคำลงในหน่วยความจำจะสร้างวัตถุชนิดต่างๆที่แสดงถึงองค์ประกอบของเ รันของข้อความย่อหน้าตารางหรือส่วนทุกโหนดและแม้แต่เอกสารเองเป็นโหนด Aspose.Wordsกำหนดคลาสสำหรับทุกชนิดโหนดเอกสาร.
โครงสร้างเอกสารในAspose.Wordsตามรูปแบบการออกแบบคอมโพสิต:
- คลาสโหนดทั้งหมดในที่สุดได้มาจากคลาสของNodeซึ่งเป็นคลาสฐานในโมเดลอ็อบเจ็กต์เอกสารAspose.Words.
- โหนดที่สามารถประกอบด้วยโหนดอื่นเช่นSectionหรือParagraphได้มาจากคลาสของCompositeNodeซึ่งจะมาจากคลาสของNode.
แผนภาพที่ให้ไว้ด้านล่างแสดงการสืบทอดระหว่างคลาสโหนดของโมเดลออบเจกต์เอกสารAspose.Words(DOM) ชื่อของชั้นเรียนนามธรรมอยู่ในตัวเอียง.
Node
.
ลองดูตัวอย่าง รูปภาพต่อไปนี้แสดงเอกสารMicrosoft Wordที่มีเนื้อหาประเภทต่างๆ.
เมื่ออ่านเอกสารข้างต้นในAspose.WordsDOMต้นไม้ของออบเจกต์จะถูกสร้างขึ้นตามที่แสดงในสคีมาข้างล่าง.
Document, Section, Paragraph, Table, Shape, Run, และวงรีอื่นๆทั้งหมดในแผนภาพเป็นAspose.Wordsวัตถุที่แสดงถึงองค์ประกอบของเอกสารคำ.
รับNode
ประเภท
แม้ว่าคลาส Node จะเพียงพอที่จะแยกแยะโหนดต่างๆ ออกจากกัน แต่ Aspose.Words ก็มีการแจงนับ NodeType เพื่อลดความซับซ้อนของงาน API บางอย่าง เช่น การเลือกโหนดประเภทเฉพาะ.
ชนิดของแต่ละโหนดสามารถรับได้โดยใช้คุณสมบัติNodeType คุณสมบัตินี้ส่งคืนค่าการแจงนับNodeType ตัวอย่างเช่นโหนดย่อหน้าแสดงโดยParagraphคลาสส่งคืนNodeType.Paragraphและโหนดตารางที่แสดงโดยTableคลาสส่งคืนNodeType.Table.
ตัวอย่างต่อไปนี้แสดงวิธีรับชนิดโหนดโดยใช้การแจงนับNodeType:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
// Returns NodeType.Document | |
int type = doc.getNodeType(); |
เอกสารต้นไม้นำทาง
Aspose.Wordsแทนเอกสารเป็นโหนดทรีซึ่งช่วยให้คุณสามารถนำทางระหว่างโหนด ส่วนนี้อธิบายวิธีสำรวจและนำทางแผนผังเอกสารในAspose.Words.
กสารแผนผังโหนดจะปรากฏตรงตามที่แสดงในAspose.Words.
ความสัมพันธ์ของโหนดเอกสาร
โหนดในต้นไม้มีความสัมพันธ์ระหว่างพวกเขา:
- โหนดที่มีโหนดอื่นคือparent.
- โหนดที่มีอยู่ในโหนดพาเรนต์คือโหนดลูกchild.ของพาเรนต์เดียวกันคือโหนดsibling.
- โหนดrootคือโหนดDocumentเสมอ.
โหนดที่ประกอบด้วยโหนดอื่นได้มาจากคลาสของCompositeNodeและโหนดทั้งหมดได้มาจากคลาสของNode สองคลาสฐานมีวิธีการทั่วไปและคุณสมบัติสำหรับการนำทางโครงสร้างต้นไม้และการปรับ.
ไดอะแกรมออบเจกต์UMLต่อไปนี้แสดงโหนดหลายโหนดของเอกสารตัวอย่างและความสัมพันธ์:
เอกสารเป็นเจ้าของโหนด
กโครงสร้างทั้งเอกสารสำคัญเช่นลักษณะและรายการจะถูกเก็บไว้ในโหนดDocument ตัวอย่างเช่นเป็นไปไม่ได้ที่จะมีParagraphโดยไม่มีDocumentเนื่องจากแต่ละย่อหน้ามีสไตล์ที่กำหนดซึ่งถูกกำหน กฏนี้ใช้เมื่อสร้างโหนดใหม่ การเพิ่มParagraphใหม่โดยตรงไปยังDOMต้องใช้วัตถุเอกสารที่ส่งผ่านไปยังคอนสตรัคเตอร์.
เมื่อสร้างย่อหน้าใหม่โดยใช้DocumentBuilderตัวสร้างจะมีคลาสของDocumentที่เชื่อมโยงกับมันผ่านคุณสมบัติDocumentBuilder.Documentเสมอ.
ตัวอย่างรหัสต่อไปนี้แสดงให้เห็นว่าเมื่อมีการสร้างโหนดใดๆเอกสารที่จะเป็นเจ้าของโห:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open a file from disk. | |
Document doc = new Document(); | |
// Creating a new node of any type requires a document passed into the constructor. | |
Paragraph para = new Paragraph(doc); | |
// The new paragraph node does not yet have a parent. | |
System.out.println("Paragraph has no parent node: " + (para.getParentNode() == null)); | |
// But the paragraph node knows its document. | |
System.out.println("Both nodes' documents are the same: " + (para.getDocument() == doc)); | |
// The fact that a node always belongs to a document allows us to access and modify | |
// properties that reference the document-wide data such as styles or lists. | |
para.getParagraphFormat().setStyleName("Heading 1"); | |
// Now add the paragraph to the main text of the first section. | |
doc.getFirstSection().getBody().appendChild(para); | |
// The paragraph node is now a child of the Body node. | |
System.out.println("Paragraph has a parent node: " + (para.getParentNode() != null)); |
โหนดแม่
แต่ละโหนดมีพาเรนต์ที่ระบุโดยคุณสมบัติParentNode โหนดไม่มีโหนดพาเรนต์นั่นคือParentNodeเป็นโมฆะในกรณีต่อไปนี้:
- โหนดเพิ่งถูกสร้างขึ้นและยังไม่ได้ถูกเพิ่มไปยังแผนผัง.
- โหนดถูกเอาออกจากต้นไม้.
- นี่คือโหนดรากDocumentซึ่งมักจะมีโหนดพาเรนต์โมฆะ.
คุณสามารถลบโหนดออกจากพาเรนต์ได้โดยเรียกวิธีการRemoveตัวอย่างรหัสต่อไปนี้แสดงวิธีการเข้าถึงโหนดพาเรนต์:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Create a new empty document. It has one section. | |
Document doc = new Document(); | |
// The section is the first child node of the document. | |
Node section = doc.getFirstChild(); | |
// The section's parent node is the document. | |
System.out.println("Section parent is the document: " + (doc == section.getParentNode())); |
โหนดเด็ก
วิธีที่มีประสิทธิภาพมากที่สุดในการเข้าถึงโหนดลูกของCompositeNodeคือผ่านคุณสมบัติFirstChildและLastChildที่ส่งคืนโหนดลูก หากไม่มีโหนดย่อยคุณสมบัติเหล่านี้จะกลับnull.
CompositeNode
ถ้าโหนดไม่มีลูกน้อยคุณสมบัติChildNodesจะส่งคืนคอลเล็กชันที่ว่างเปล่า คุณสามารถตรวจสอบว่าCompositeNodeมีโหนดย่อยใดๆโดยใช้คุณสมบัติHasChildNodes.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการระบุโหนดย่อยทันทีของ CompositeNode
โดยใช้ตัวระบุที่ให้มาโดยคอลเลกชัน ChildNodes
:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
Paragraph paragraph = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); | |
NodeCollection children = paragraph.getChildNodes(); | |
for (Node child : (Iterable<Node>) children) { | |
// Paragraph may contain children of various types such as runs, shapes and so on. | |
if (child.getNodeType() == NodeType.RUN) { | |
// Say we found the node that we want, do something useful. | |
Run run = (Run) child; | |
System.out.println(run.getText()); | |
} | |
} |
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการระบุโหนดย่อยในทันทีของCompositeNode
โดยใช้การเข้าถึงการทำดัชนี:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
Paragraph paragraph = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); | |
NodeCollection children = paragraph.getChildNodes(); | |
for (int i = 0; i < children.getCount(); i++) { | |
Node child = children.get(i); | |
// Paragraph may contain children of various types such as runs, shapes and so on. | |
if (child.getNodeType() == NodeType.RUN) { | |
// Say we found the node that we want, do something useful. | |
Run run = (Run) child; | |
System.out.println(run.getText()); | |
} | |
} |
โหนดพี่น้อง
คุณสามารถขอรับโหนดที่นำหน้าหรือติดตามโหนดเฉพาะโดยใช้คุณสมบัติPreviousSiblingและNextSiblingตามลำดับ ถ้าโหนดเป็นลูกคนสุดท้ายของพาเรนต์คุณสมบัติNextSiblingคือnull ในทางกลับกันถ้าโหนดเป็นลูกคนแรกของแม่คุณสมบัติPreviousSiblingคือnull.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเยี่ยมชมโหนดลูกโดยตรงและโดยอ้อมทั้งหมดของโหนดค:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static void main(String[] args) throws Exception { | |
String dataDir = Utils.getSharedDataDir(ChildNodes.class) + "DocumentObjectModel/"; | |
recurseAllNodes(dataDir); | |
} | |
public static void recurseAllNodes(String dataDir) throws Exception { | |
// Open a document | |
Document doc = new Document(dataDir + "Node.RecurseAllNodes.doc"); | |
// Invoke the recursive function that will walk the tree. | |
traverseAllNodes(doc); | |
} | |
/** | |
* A simple function that will walk through all children of a specified node | |
* recursively and print the type of each node to the screen. | |
*/ | |
public static void traverseAllNodes(CompositeNode parentNode) throws Exception { | |
// This is the most efficient way to loop through immediate children of a node. | |
for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) { | |
// Do some useful work. | |
System.out.println(Node.nodeTypeToString(childNode.getNodeType())); | |
// Recurse into the node if it is a composite node. | |
if (childNode.isComposite()) | |
traverseAllNodes((CompositeNode) childNode); | |
} | |
} |
การเข้าถึงที่พิมพ์ไปยังโหนดเด็กและแม่
จนถึงขณะนี้เราได้กล่าวถึงคุณสมบัติที่ส่งคืนหนึ่งในชนิดฐาน-NodeหรือCompositeNode แต่บางครั้งมีสถานการณ์ที่คุณอาจต้องโยนค่าไปยังคลาสโหนดที่เฉพาะเจาะจงเช่นRunหรือParagraph นั่นคือคุณไม่สามารถหนีจากการหล่อเมื่อทำงานกับAspose.WordsDOMซึ่งเป็นคอมโพสิต.
เพื่อลดความจำเป็นในการแคสต์คลาสAspose.Wordsส่วนใหญ่มีคุณสมบัติและคอลเล็กชันที่ให้การเข้าถึงที่ มีสามรูปแบบพื้นฐานของการเข้าถึงที่พิมพ์เป็น:
- โหนดพาเรนต์แสดงคุณสมบัติที่พิมพ์FirstXXXและLastXXX ตัวอย่างเช่นDocumentมีFirstSectionและLastSectionคุณสมบัติ ในทำนองเดียวกันTableมีคุณสมบัติเช่นFirstRow,LastRowและอื่นๆ.
- โหนดพาเรนต์แสดงคอลเล็กชันที่พิมพ์ของโหนดย่อยเช่นDocument.Sections,Body.Paragraphsและโหนดอื่นๆ.
- โหนดย่อยให้สิทธิ์การเข้าถึงที่พิมพ์ไปยังพาเรนต์เช่นRun.ParentParagraph,Paragraph.ParentSectionและอื่นๆ.
มบัติทั่วไปที่สืบทอดมาจากNode.ParentNodeและCompositeNode.FirstChild.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการใช้คุณสมบัติที่พิมพ์เพื่อเข้าถึงโหนดของแผนผังเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
// Quick typed access to the first child Section node of the Document. | |
Section section = doc.getFirstSection(); | |
// Quick typed access to the Body child node of the Section. | |
Body body = section.getBody(); | |
// Quick typed access to all Table child nodes contained in the Body. | |
TableCollection tables = body.getTables(); | |
for (Table table : tables) { | |
// Quick typed access to the first row of the table. | |
if (table.getFirstRow() != null) | |
table.getFirstRow().remove(); | |
// Quick typed access to the last row of the table. | |
if (table.getLastRow() != null) | |
table.getLastRow().remove(); | |
} |