質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Google Apps Script

Google Apps ScriptはGoogleの製品と第三者のサービスでタスクを自動化するためのJavaScriptのクラウドのスクリプト言語です。

Q&A

解決済

1回答

920閲覧

gasでXMLをXmlServiceしてスプレッドシートにしたい。子要素取得困ってます。

BBA

総合スコア60

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Google Apps Script

Google Apps ScriptはGoogleの製品と第三者のサービスでタスクを自動化するためのJavaScriptのクラウドのスクリプト言語です。

0グッド

0クリップ

投稿2022/12/20 05:34

編集2022/12/21 01:28

XMLのスクレイピングをGASでしてます

XmlからXmlServiceを使用して取得してスプレッドシートに保存できるコードを書いています
取得したいXML

要素の取得に条件が違う二つを一緒に取得したいです。

実現したいこと

MainProvision→Chapter→Section→Articleの要素取得と
MainProvision→Chapter→Articleの要素取得を
一緒にしたいです。

取得したいXML sectionある時 <Chapter Num="2"> <ChapterTitle>第二章 一般構造</ChapterTitle> <Section Num="1"> <SectionTitle>第一節 採光に必要な開口部</SectionTitle> <Article Num="19"> <ArticleCaption>(学校、病院、児童福祉施設等の居室の採光)</ArticleCaption> <ArticleTitle>第十九条</ArticleTitle> <Paragraph Num="1"> <ParagraphNum/> <ParagraphSentence> <Sentence Num="1" WritingMode="vertical">法第二十八条第一項(法第八十七条第三項において準用する場合を含む。以下この条及び次条において同じ。)の政令で定める建築物は、児童福祉施設(幼保連携型認定こども園を除く。)、助産所、身体障害者社会参加支援施設(補装具製作施設及び視聴覚障害者情報提供施設を除く。)、保護施設(医療保護施設を除く。)、婦人保護施設、老人福祉施設、有料老人ホーム、母子保健施設、障害者支援施設、地域活動支援センター、福祉ホーム又は障害福祉サービス事業(生活介護、自立訓練、就労移行支援又は就労継続支援を行う事業に限る。)の用に供する施設(以下「児童福祉施設等」という。)とする。</Sentence> </ParagraphSentence> </Paragraph>
取得したいXML sectionない時 <Chapter Num="4"> <ChapterTitle>第四章 耐火構造、準耐火構造、防火構造、防火区画等</ChapterTitle> <Article Num="107"> <ArticleCaption>(耐火性能に関する技術的基準)</ArticleCaption> <ArticleTitle>第百七条</ArticleTitle> <Paragraph Num="1"> <ParagraphNum/> <ParagraphSentence> <Sentence Num="1" WritingMode="vertical">法第二条第七号の政令で定める技術的基準は、次に掲げるものとする。</Sentence> </ParagraphSentence> <Item Num="1"> <ItemTitle>一</ItemTitle> <ItemSentence> <Sentence Num="1" WritingMode="vertical">次の表に掲げる建築物の部分にあつては、当該部分に通常の火災による火熱がそれぞれ次の表に掲げる時間加えられた場合に、構造耐力上支障のある変形、溶融、破壊その他の損傷を生じないものであること。</Sentence> </ItemSentence>

条件を2つ指定してeachできるようにしたい

sectionでもeachが必要です。
子要素をifで部分的にはできましたがsectionは子要素の多い親要素で書き方がわかりません。

該当のソースコード

function re() { var sheet = SpreadsheetApp.openById("********").getSheetByName("****"); let url = 'https://elaws.e-gov.go.jp/api/1/lawdata/昭和二十五年政令第三百三十八号'; let xml = UrlFetchApp.fetch(url).getContentText(); let document = XmlService.parse(xml); let root = document.getRootElement(); var lastrow = sheet.getLastRow(); var recordrow = lastrow + 1; const applData = root.getChild("ApplData"); const lawFullText = applData.getChild("LawFullText"); const law = lawFullText.getChild("Law"); const lawBody = law.getChild("LawBody"); const mainProvision = lawBody.getChild("MainProvision"); const chapters = mainProvision.getChildren("Chapter"); chapters.forEach(function(chapter) { const articles = chapter.getChildren("Article"); ***ここにsectionのforEachを追加?ある時とない時の条件設定がわかりません*** articles.forEach(function(article){   const articleCaption = article.getChild("ArticleCaption"); const articleTitle = article.getChild("ArticleTitle"); const paragraphs = article.getChildren("Paragraph"); if (articleCaption != null){ sheet.getRange("A" + recordrow).setValue(articleCaption.getText()); } sheet.getRange("B" + recordrow).setValue(articleTitle.getText());    if (paragraphs != null){ paragraphs.forEach(function(paragraph){ const paragraphNum = paragraph.getChild("ParagraphNum"); const paragraphSentence = paragraph.getChild("ParagraphSentence"); sheet.getRange("C" + recordrow).setValue(paragraphNum.getValue()); sheet.getRange("F" + recordrow).setValue(paragraphSentence.getValue()); recordrow = recordrow + 1; const items = paragraph.getChildren("Item"); if (items !== null) { items.forEach(function (item) { const itemtitle = item.getChild("ItemTitle"); sheet.getRange("D" + recordrow).setValue(itemtitle.getText()); const itemSentence = item.getChild("ItemSentence");     sheet.getRange("F" + recordrow).setValue(itemSentence.getValue());        recordrow = recordrow + 1; const columns = itemSentence.getChildren("Column"); if (columns !== null) { columns.forEach(function (column) {      sheet.getRange("F" + recordrow).setValue(column.getText()); }) } const subitem1s = item.getChildren("Subitem1"); if (subitem1s !== null) { subitem1s.forEach(function (subitem1) { const subitem1Title = subitem1.getChild("Subitem1Title");     sheet.getRange("D" + recordrow).setValue(subitem1Title.getText()); const subitem1Sentences = subitem1.getChildren("Subitem1Sentence"); subitem1Sentences.forEach(function (subitem1Sentence) {      sheet.getRange("F" + recordrow).setValue(subitem1Sentence.getValue()); }); const subitem2s = subitem1.getChildren("Subitem2"); if (subitem2s !== null) { subitem2s.forEach(function (subitem2) { const subitem2Title = subitem2.getChild("Subitem2Title"); sheet.getRange("D" + recordrow).setValue(subitem2Title.getText()); const subitem2Sentences = subitem2.getChildren("Subitem2Sentence"); subitem2Sentences.forEach(function (subitem2Sentence) { sheet.getRange("F" + recordrow).setValue(subitem2Sentence.getValue()); }); const subitem3s = subitem2.getChildren("Subitem3"); if (subitem3s !== null) { subitem3s.forEach(function (subitem3) { const subitem3Title = subitem3.getChild("Subitem3Title");         sheet.getRange("D" + recordrow).setValue(subitem3Title.getText()); const subitem3Sentences = subitem3.getChildren("Subitem3Sentence"); subitem3Sentences.forEach(function (subitem3Sentence) {       sheet.getRange("F" + recordrow).setValue(subitem3Sentence.getValue()); });    }) }    }) } }) } }) } }) } }) }) }

試したこと

if (section != null){ }

これだったら書かないで取得できるのと同じでした。

試してみた追加コード
実行したら何も反応せず終わりました。

gas

1上記と同じ 2 3 const applData = root.getChild("ApplData"); 4 const lawFullText = applData.getChild("LawFullText"); 5 const law = lawFullText.getChild("Law"); 6 const lawBody = law.getChild("LawBody"); 7 const mainProvision = lawBody.getChild("MainProvision"); 8 9 const chapters = mainProvision.getChildren("Chapter"); 10chapters.forEach(function(chapter) { 11 12****追加した部分**** 13 var sections = chapter.getChild("Section"); 14 if (sections != null,sections = null){ 15sections.forEach(function(section) { 16 sheet.getRange("A" + recordrow).setValue(section.getText()); 17 var articles = [chapter.getChildren("Article"), section.getChildren("Article")] 18******** 19articles.forEach(function(article){ 20  const articleCaption = article.getChild("ArticleCaption"); 21 const articleTitle = article.getChild("ArticleTitle"); 22 const paragraphs = article.getChildren("Paragraph"); 23 if (articleCaption != null){ 24 sheet.getRange("A" + recordrow).setValue(articleCaption.getText()); 25 } 26 sheet.getRange("B" + recordrow).setValue(articleTitle.getText()); 27 28以下同じ

補足情報

長文コードですみません。どなたかご教授いただけないでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

下記のように、Article取得部分を関数に切り出して整理してはどうでしょうか。

考え方としては、

js

1 const sections = chapter.getChildren("Section"); 2 if (sections.length > 0) { // ********ここ******* 3 sections.forEach(function (section) { 4 section 内の article 取得 5 }); 6 } else { 7 chapter 内の article取得 8 }

のように、if と elseを使います。

具体的に言うと if で比較しているのは、「指定した子要素の数(length)が0より大きいか否か」という点です。
たとえば、上のスニペットの1行目
const sections = chapter.getChildren("Section");
は、chapterの子要素のうち「Section」という名前を持つ要素たちを抽出して section という変数に格納する、という意味です。

比較した結果がtrueであれば、要素の中に指定した子要素があると判断して、その子要素を順次処理します。

比較した結果がfalseであれば、要素の中にある子要素は article と見なし、articleを取得する関数を呼び出します。

なお、質問文に記載の法律の場合、
MainProvision→Chapter→Section→Article、
MainProvision→Chapter→Article
だけではなく
MainProvision→Chapter→Section→Subsection→Article
のパターンも処理しておかないと一部の条文が取得できないため、対応しています。

js

1var sheet = SpreadsheetApp.openById("********").getSheetByName("****"); 2var lastrow = sheet.getLastRow(); 3var recordrow = lastrow + 1; 4 5 6function main() { 7 8 let url = 'https://elaws.e-gov.go.jp/api/1/lawdata/昭和二十五年政令第三百三十八号'; 9 let xml = UrlFetchApp.fetch(url).getContentText(); 10 let document = XmlService.parse(xml); 11 let root = document.getRootElement(); 12 13 const applData = root.getChild("ApplData"); 14 const lawFullText = applData.getChild("LawFullText"); 15 const law = lawFullText.getChild("Law"); 16 const lawBody = law.getChild("LawBody"); 17 const mainProvision = lawBody.getChild("MainProvision"); 18 19 const chapters = mainProvision.getChildren("Chapter"); 20 chapters.forEach(function (chapter) { 21 const sections = chapter.getChildren("Section"); 22 if (sections.length > 0) { 23 // MainProvision→Chapter→Section 24 sections.forEach(function (section) { 25 const subsections = section.getChildren("Subsection"); 26 if (subsections.length > 0) { 27 // MainProvision→Chapter→Section→Subsection→Article 28 subsections.forEach(function (subsection) { 29 sheet.getRange("A" + recordrow).setValue(subsection.getText()); 30 const articles = subsection.getChildren("Article"); 31 processArticles(articles); 32 recordrow = recordrow + 1; 33 }); 34 } else { 35 // MainProvision→Chapter→Section→Article 36 sheet.getRange("A" + recordrow).setValue(section.getText()); 37 const articles = section.getChildren("Article"); 38 processArticles(articles); 39 recordrow = recordrow + 1; 40 } 41 }); 42 } else { 43 // MainProvision→Chapter→Article 44 const articles = chapter.getChildren("Article"); 45 processArticles(articles); 46 } 47 }) 48} 49 50 51function processArticles(articles) { 52 articles.forEach(function (article) { 53 const articleCaption = article.getChild("ArticleCaption"); 54 const articleTitle = article.getChild("ArticleTitle"); 55 56 const paragraphs = article.getChildren("Paragraph"); 57 if (articleCaption != null) { 58 sheet.getRange("A" + recordrow).setValue(articleCaption.getText()); 59 console.log(articleCaption.getText()) 60 } 61 sheet.getRange("B" + recordrow).setValue(articleTitle.getText()); 62 63 64 if (paragraphs != null) { 65 paragraphs.forEach(function (paragraph) { 66 const paragraphNum = paragraph.getChild("ParagraphNum"); 67 const paragraphSentence = paragraph.getChild("ParagraphSentence"); 68 sheet.getRange("C" + recordrow).setValue(paragraphNum.getValue()); 69 sheet.getRange("F" + recordrow).setValue(paragraphSentence.getValue()); 70 recordrow = recordrow + 1; 71 const items = paragraph.getChildren("Item"); 72 73 if (items !== null) { 74 items.forEach(function (item) { 75 const itemtitle = item.getChild("ItemTitle"); 76 sheet.getRange("D" + recordrow).setValue(itemtitle.getText()); 77 const itemSentence = item.getChild("ItemSentence"); 78 sheet.getRange("F" + recordrow).setValue(itemSentence.getValue()); 79 recordrow = recordrow + 1; 80 const columns = itemSentence.getChildren("Column"); 81 if (columns !== null) { 82 columns.forEach(function (column) { 83 sheet.getRange("F" + recordrow).setValue(column.getText()); 84 }) 85 } 86 const subitem1s = item.getChildren("Subitem1"); 87 if (subitem1s !== null) { 88 subitem1s.forEach(function (subitem1) { 89 const subitem1Title = subitem1.getChild("Subitem1Title"); 90 sheet.getRange("D" + recordrow).setValue(subitem1Title.getText()); 91 const subitem1Sentences = subitem1.getChildren("Subitem1Sentence"); 92 subitem1Sentences.forEach(function (subitem1Sentence) { 93 sheet.getRange("F" + recordrow).setValue(subitem1Sentence.getValue()); 94 }); 95 const subitem2s = subitem1.getChildren("Subitem2"); 96 if (subitem2s !== null) { 97 subitem2s.forEach(function (subitem2) { 98 const subitem2Title = subitem2.getChild("Subitem2Title"); 99 sheet.getRange("D" + recordrow).setValue(subitem2Title.getText()); 100 const subitem2Sentences = subitem2.getChildren("Subitem2Sentence"); 101 subitem2Sentences.forEach(function (subitem2Sentence) { 102 sheet.getRange("F" + recordrow).setValue(subitem2Sentence.getValue()); 103 }); 104 const subitem3s = subitem2.getChildren("Subitem3"); 105 if (subitem3s !== null) { 106 subitem3s.forEach(function (subitem3) { 107 const subitem3Title = subitem3.getChild("Subitem3Title"); 108 sheet.getRange("D" + recordrow).setValue(subitem3Title.getText()); 109 const subitem3Sentences = subitem3.getChildren("Subitem3Sentence"); 110 subitem3Sentences.forEach(function (subitem3Sentence) { 111 sheet.getRange("F" + recordrow).setValue(subitem3Sentence.getValue()); 112 }); 113 }) 114 } 115 }) 116 } 117 }) 118 } 119 }) 120 } 121 }) 122 } 123 }) 124}

投稿2022/12/21 14:57

編集2022/12/21 15:15
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

BBA

2022/12/22 08:51

すごいです。。。 if (sections.length > 0)  lengthにする発想がなかったです。。。 アンサーのコードで条文が取れました。 すごい。、、ありがとうございました!!!!!!!!!!!
BBA

2022/12/22 08:52

Subsectionも考慮してくださって感謝です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問