ฉันสามารถคัดลอกโหนดจากไฟล์ XML หนึ่งไปยังอีกไฟล์หนึ่งได้โดยใช้ "org.w3c.dom.Document.importNode(Node importNode, boolean deep)"
อย่างไรก็ตาม ฉัน ดูเหมือนจะไม่สามารถเปลี่ยนชื่อองค์ประกอบที่ฉันกำลังคัดลอกได้
ฉันมีบางอย่างเช่น:
File1.xml
<SomeCustomNode randomAttribute="aValue" another="10/10/2010">
<Information>
<Yellow name="banana"/>
<Orange name="orange"/>
<Red name="strawberry"/>
</Information>
<Some>
<IgnoredNode/>
</Some>
</SomeCustomNode>
และอะไรทำนองนี้:
FileList.xml
<ListOfNodes date="12/10/2010">
<aCopy name="fruit" version="10">
<Yellow name="banana"/>
<Orange name="orange"/>
<Red name="strawberry"/>
</aCopy>
<aCopy name="vegetables" version="3">
<Yellow name="sweetcorn"/>
<Orange name="carrot"/>
<Red name="tomato"/>
</aCopy>
</ListOfNodes>
ดังนั้น สิ่งที่ฉันกำลังทำคือนำโหนด (และลูก ๆ ) จาก File1.xml
และแทรกลงใน FileList.xml
แต่เปลี่ยนชื่อ Element
และเพิ่มแอตทริบิวต์สองสามรายการให้กับองค์ประกอบ
ข้อมูล กลายเป็น aCopy name="fruit" version="10"
ขณะนี้ฉันกำลังใช้นิพจน์ XPath เพื่อรับโหนด ข้อมูล เป็น NodeList (ผลลัพธ์เพียง 1 รายการ) จากนั้นนำเข้าสิ่งนั้นลงใน File2 ดังนี้:
Document docFile1 = XMLDocumentStore.getDoc("/path/to/File1.xml");
Document docFileList = XMLDocumentStore.getDoc("/path/to/FileList.xml");
NodeList result = XPathAPI.selectNodeList(docFile1.getFirstChild(), ".//Information");
Node importNode = docFileList.importNode(result.item(0), true);
// We want to replace aCopy fruit with the updated version found in File1
NodeList fruitNode = XPathAPI.selectNodeList(docFileList.getFirstChild(), ".//aCopy[@name=\"fruit\"]");
Node replaceNode = fruitNode.item(0).getParentNode().replaceChild(importNode, fruitNode.item(0)); // probably a better way to do this
// Now we want to replace the Element name as it is still set to Information
docFileList.renameNode(replaceNode, null, "aCopy"); // Error: oracle.xml.parser.v2.XMLDOMException: cannot add attribute belonging to another element
ฉันได้รับข้อผิดพลาดอื่นๆ หากฉันย้ายโค้ดไปรอบๆ เล็กน้อย เช่น: ไม่สามารถลบหรือแทนที่โหนดได้ ไม่ใช่โหนดลูกของโหนดปัจจุบัน ฯลฯ
จะดีกว่านี้ผ่าน XSLT หรือไม่ ทั้งหมดที่ฉันทำคือนำโหนดเฉพาะ (และเป็นโหนดย่อย) และใส่ลงในไฟล์ XML อื่น แต่เปลี่ยนชื่อองค์ประกอบและเพิ่มแอตทริบิวต์ 2 รายการ (ด้วยค่า) มันจะเป็นโหนดเดียวกันสำหรับแต่ละไฟล์ (File1 ... File###) และจะถูกเปลี่ยนชื่อในลักษณะเดียวกัน ค่าแอตทริบิวต์จะถูกนำมาจากไฟล์ต้นฉบับ (เช่น File1.xml สำหรับตัวอย่างของฉัน) และส่วนย่อย โหนดไม่ได้รับการเปลี่ยนแปลง (สีเหลือง, สีส้ม, สีแดงในตัวอย่างของฉัน)
ไชโย!