이 XML에 새로운 category
노드를 입력하는 것이 저의 목표입니다. 내가Nokogiri를 사용하여 새로운 형제 요소를 추가하려면 어떻게해야합니까?
categories = doc.css("category")
categories.after "<test></test>"
을하려고 할 때
<<?xml version="1.0" encoding="UTF-8"?>
<categories xmlns="CategoryList.xsd" lastSavedTime="2014-05-02T20:07:40.5903428Z">
<category renameOnFirstUse="1" name="Red category" color="0" keyboardShortcut="0" lastTimeUsedNotes="2014-05-02T20:07:40.5279436Z" lastTimeUsedJournal="2014-05-02T20:07:40.5279436Z" lastTimeUsedContacts="2014-05-02T20:07:40.5279436Z" lastTimeUsedTasks="2014-05-02T20:07:40.5279436Z" lastTimeUsedCalendar="2014-05-02T20:07:40.5279436Z" lastTimeUsedMail="2014-05-02T20:07:40.5279436Z" lastTimeUsed="2014-05-02T20:07:40.5279436Z" guid="{4f331f68-b735-4797-a0c4-9b02e13d079f}">
<category renameOnFirstUse="0" name="test" color="10" keyboardShortcut="0" lastTimeUsedNotes="2014-05-02T20:07:40.5279436Z" lastTimeUsedJournal="2014-05-02T20:07:40.5279436Z" lastTimeUsedContacts="2014-05-02T20:07:40.5279436Z" lastTimeUsedTasks="2014-05-02T20:07:40.5279436Z" lastTimeUsedCalendar="2014-05-02T20:07:40.5279436Z" lastTimeUsedMail="2014-05-02T20:07:40.5279436Z" lastTimeUsed="2014-05-02T20:07:40.5279436Z" guid="{4452319c-0253-4764-a72f-065e5a4af017}" />
</category>
<category renameOnFirstUse="1" name="Orange category" color="1" keyboardShortcut="0" lastTimeUsedNotes="2014-05-02T20:07:40.5279436Z" lastTimeUsedJournal="2014-05-02T20:07:40.5279436Z" lastTimeUsedContacts="2014-05-02T20:07:40.5279436Z" lastTimeUsedTasks="2014-05-02T20:07:40.5279436Z" lastTimeUsedCalendar="2014-05-02T20:07:40.5279436Z" lastTimeUsedMail="2014-05-02T20:07:40.5279436Z" lastTimeUsed="2014-05-02T20:07:40.5279436Z" guid="{eb64b97a-d5a2-41ed-a0ed-dc60fe0601e2}" />
<category renameOnFirstUse="1" name="Yellow category" color="3" keyboardShortcut="0" lastTimeUsedNotes="2014-05-02T20:07:40.5279436Z" lastTimeUsedJournal="2014-05-02T20:07:40.5279436Z" lastTimeUsedContacts="2014-05-02T20:07:40.5279436Z" lastTimeUsedTasks="2014-05-02T20:07:40.5279436Z" lastTimeUsedCalendar="2014-05-02T20:07:40.5279436Z" lastTimeUsedMail="2014-05-02T20:07:40.5279436Z" lastTimeUsed="2014-05-02T20:07:40.5279436Z" guid="{0010faba-dd02-44fb-afbe-3fd5ac2d4f6f}" />
<category renameOnFirstUse="1" name="Green category" color="4" keyboardShortcut="0" lastTimeUsedNotes="2014-05-02T20:07:40.5279436Z" lastTimeUsedJournal="2014-05-02T20:07:40.5279436Z" lastTimeUsedContacts="2014-05-02T20:07:40.5279436Z" lastTimeUsedTasks="2014-05-02T20:07:40.5279436Z" lastTimeUsedCalendar="2014-05-02T20:07:40.5279436Z" lastTimeUsedMail="2014-05-02T20:07:40.5279436Z" lastTimeUsed="2014-05-02T20:07:40.5279436Z" guid="{38f5e37c-0fe7-41c4-b265-7b30b9a6fcfa}" />
<category renameOnFirstUse="1" name="Blue category" color="7" keyboardShortcut="0" lastTimeUsedNotes="2014-05-02T20:07:40.5279436Z" lastTimeUsedJournal="2014-05-02T20:07:40.5279436Z" lastTimeUsedContacts="2014-05-02T20:07:40.5279436Z" lastTimeUsedTasks="2014-05-02T20:07:40.5279436Z" lastTimeUsedCalendar="2014-05-02T20:07:40.5279436Z" lastTimeUsedMail="2014-05-02T20:07:40.5279436Z" lastTimeUsed="2014-05-02T20:07:40.5279436Z" guid="{7fb0e73e-4193-4836-b9e9-c2017168418a}" />
<category renameOnFirstUse="1" name="Purple category" color="8" keyboardShortcut="0" lastTimeUsedNotes="2014-05-02T20:07:40.5279436Z" lastTimeUsedJournal="2014-05-02T20:07:40.5279436Z" lastTimeUsedContacts="2014-05-02T20:07:40.5279436Z" lastTimeUsedTasks="2014-05-02T20:07:40.5279436Z" lastTimeUsedCalendar="2014-05-02T20:07:40.5279436Z" lastTimeUsedMail="2014-05-02T20:07:40.5279436Z" lastTimeUsed="2014-05-02T20:07:40.5279436Z" guid="{0ed51616-504f-4415-936c-a97658aa2458}" />
</categories>
는하지만 얻을 : nokogiri/xml/node_set.rb:59:in 'after': undefined method 'after' for nil:NilClass (NoMethodError)
나는 모든 범주가 categories
변수로 읽어지고 있음을 알고 내가 그들을 인쇄 할 수 있기 때문이다. XML 문서에 새 범주를 추가 할 수 없었습니다.
나는 또한 doc.at('category').add_next_sibling("<AnotherNode>13</AnotherNode>")
을 시도했다. 같은 오류가 발생합니다 : undefined method 'add_next_sibling' for nil:NilClass (NoMethodError)
. 내가 도대체 뭘 잘못하고있는 겁니까?
"[mcve]"를 (를) 읽으십시오. 우리는 여러분이 시도한 것에 대한 논평보다는 코드의 작은 예를보고 싶습니다. –