將 XML 的 snippits 新增到當前的 XMLDocument
樣本資料
XML 文件
首先,讓我們在當前目錄中定義一個名為books.xml 的示例 XML 文件 :
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>Of Mice And Men</title>
<author>John Steinbeck</author>
<pageCount>187</pageCount>
<publishers>
<publisher>
<isbn>978-88-58702-15-4</isbn>
<name>Pascal Covici</name>
<year>1937</year>
<binding>Hardcover</binding>
<first>true</first>
</publisher>
<publisher>
<isbn>978-05-82461-46-8</isbn>
<name>Longman</name>
<year>2009</year>
<binding>Hardcover</binding>
</publisher>
</publishers>
<characters>
<character name="Lennie Small" />
<character name="Curley's Wife" />
<character name="George Milton" />
<character name="Curley" />
</characters>
<film>True</film>
</book>
<book>
<title>The Hunt for Red October</title>
<author>Tom Clancy</author>
<pageCount>387</pageCount>
<publishers>
<publisher>
<isbn>978-08-70212-85-7</isbn>
<name>Naval Institute Press</name>
<year>1984</year>
<binding>Hardcover</binding>
<first>true</first>
</publisher>
<publisher>
<isbn>978-04-25083-83-3</isbn>
<name>Berkley</name>
<year>1986</year>
<binding>Paperback</binding>
</publisher>
<publisher>
<isbn>978-08-08587-35-4</isbn>
<name>Penguin Putnam</name>
<year>2010</year>
<binding>Paperback</binding>
</publisher>
</publishers>
<characters>
<character name="Marko Alexadrovich Ramius" />
<character name="Jack Ryan" />
<character name="Admiral Greer" />
<character name="Bart Mancuso" />
<character name="Vasily Borodin" />
</characters>
<film>True</film>
</book>
</books>
新資料
我們想要做的是在本文件中新增一些新書,讓我們說湯姆克蘭西的愛國者遊戲 (是的,我是克蘭西作品的粉絲^ __ ^)和科幻愛好者: 銀河系漫遊指南道格拉斯亞當斯主要是因為 Zaphod Beeblebrox 閱讀起來很有趣。
不知何故,我們已經獲取了新書的資料,並將它們儲存為 PSCustomObjects 列表:
$newBooks = @(
[PSCustomObject] @{
"Title" = "Patriot Games";
"Author" = "Tom Clancy";
"PageCount" = 540;
"Publishers" = @(
[PSCustomObject] @{
"ISBN" = "978-0-39-913241-4";
"Year" = "1987";
"First" = $True;
"Name" = "Putnam";
"Binding" = "Hardcover";
}
);
"Characters" = @(
"Jack Ryan", "Prince of Wales", "Princess of Wales",
"Robby Jackson", "Cathy Ryan", "Sean Patrick Miller"
);
"film" = $True;
},
[PSCustomObject] @{
"Title" = "The Hitchhiker's Guide to the Galaxy";
"Author" = "Douglas Adams";
"PageCount" = 216;
"Publishers" = @(
[PSCustomObject] @{
"ISBN" = "978-0-33-025864-7";
"Year" = "1979";
"First" = $True;
"Name" = "Pan Books";
"Binding" = "Hardcover";
}
);
"Characters" = @(
"Arthur Dent", "Marvin", "Zaphod Beeblebrox", "Ford Prefect",
"Trillian", "Slartibartfast", "Dirk Gently"
);
"film" = $True;
}
);
模板
現在我們需要為我們的新資料定義一些骨架 XML 結構。基本上,你希望為每個資料列表建立一個框架/模板。在我們的示例中,這意味著我們需要一本書,字元和釋出者的模板。我們也可以使用它來定義一些預設值,例如 film
標籤的值。
$t_book = [xml] @'
<book>
<title />
<author />
<pageCount />
<publishers />
<characters />
<film>False</film>
</book>
'@;
$t_publisher = [xml] @'
<publisher>
<isbn/>
<name/>
<year/>
<binding/>
<first>false</first>
</publisher>
'@;
$t_character = [xml] @'
<character name="" />
'@;
我們完成了設定。
新增新資料
現在我們已經使用我們的示例資料進行了設定,讓我們將自定義物件新增到 XML 文件物件中。
# Read the xml document
$xml = [xml] Get-Content .ooks.xml;
# Let's show a list of titles to see what we've got currently:
$xml.books.book | Select Title, Author, @{N="ISBN";E={If ( $_.Publishers.Publisher.Count ) { $_.Publishers.publisher[0].ISBN} Else { $_.Publishers.publisher.isbn}}};;
# Outputs:
# title author ISBN
# ----- ------ ----
# Of Mice And Men John Steinbeck 978-88-58702-15-4
# The Hunt for Red October Tom Clancy 978-08-70212-85-7
# Let's show our new books as well:
$newBooks | Select Title, Author, @{N="ISBN";E={$_.Publishers[0].ISBN}};
# Outputs:
# Title Author ISBN
# ----- ------ ----
# Patriot Games Tom Clancy 978-0-39-913241-4
# The Hitchhiker's Guide to the Galaxy Douglas Adams 978-0-33-025864-7
# Now to merge the two:
ForEach ( $book in $newBooks ) {
$root = $xml.SelectSingleNode("/books");
# Add the template for a book as a new node to the root element
[void]$root.AppendChild($xml.ImportNode($t_book.book, $true));
# Select the new child element
$newElement = $root.SelectSingleNode("book[last()]");
# Update the parameters of that new element to match our current new book data
$newElement.title = [String]$book.Title;
$newElement.author = [String]$book.Author;
$newElement.pageCount = [String]$book.PageCount;
$newElement.film = [String]$book.Film;
# Iterate through the properties that are Children of our new Element:
ForEach ( $publisher in $book.Publishers ) {
# Create the new child publisher element
# Note the use of "SelectSingleNode" here, this allows the use of the "AppendChild" method as it returns
# a XmlElement type object instead of the $Null data that is currently stored in that leaf of the
# XML document tree
[void]$newElement.SelectSingleNode("publishers").AppendChild($xml.ImportNode($t_publisher.publisher, $true));
# Update the attribute and text values of our new XML Element to match our new data
$newPublisherElement = $newElement.SelectSingleNode("publishers/publisher[last()]");
$newPublisherElement.year = [String]$publisher.Year;
$newPublisherElement.name = [String]$publisher.Name;
$newPublisherElement.binding = [String]$publisher.Binding;
$newPublisherElement.isbn = [String]$publisher.ISBN;
If ( $publisher.first ) {
$newPublisherElement.first = "True";
}
}
ForEach ( $character in $book.Characters ) {
# Select the characters xml element
$charactersElement = $newElement.SelectSingleNode("characters");
# Add a new character child element
[void]$charactersElement.AppendChild($xml.ImportNode($t_character.character, $true));
# Select the new characters/character element
$characterElement = $charactersElement.SelectSingleNode("character[last()]");
# Update the attribute and text values to match our new data
$characterElement.name = [String]$character;
}
}
# Check out the new XML:
$xml.books.book | Select Title, Author, @{N="ISBN";E={If ( $_.Publishers.Publisher.Count ) { $_.Publishers.publisher[0].ISBN} Else { $_.Publishers.publisher.isbn}}};
# Outputs:
# title author ISBN
# ----- ------ ----
# Of Mice And Men John Steinbeck 978-88-58702-15-4
# The Hunt for Red October Tom Clancy 978-08-70212-85-7
# Patriot Games Tom Clancy 978-0-39-913241-4
# The Hitchhiker's Guide to the Galaxy Douglas Adams 978-0-33-025864-7
我們現在可以將 XML 寫入磁碟,螢幕,Web 或任何地方!
利潤
雖然這可能不是每個人的程式,我發現它可以幫助避免一大堆 [void]$xml.SelectSingleNode("/complicated/xpath/goes[here]").AppendChild($xml.CreateElement("newElementName")
然後 $xml.SelectSingleNode("/complicated/xpath/goes/here/newElementName") = $textValue
我認為示例中詳述的方法更清晰,更容易解析普通人。
改進
可以更改模板以包含具有子項的元素,而不是將每個部分作為單獨的模板分解。迴圈遍歷列表時,你只需要注意克隆前一個元素。