XMLのスクレイピングをGASでしてます
XmlからXmlServiceを使用して取得したくコードを書いています。
途中まで取得できましたが、
子要素をforeachで繰り返し処理したいのですが、
エラーになってしまいました。
ループしたい要素itemでエラーになります
実現したいこと
最終はスプレッドシートにXMLデータを保存
することです。
下記の表のようにするのが目標です
ArticleCaption | ArticleTitle | ItemTitle | Sentence |
---|---|---|---|
(業務) | 第三条 | 司法書士は、この法律の定めるところにより、他人の依頼を受けて、次に掲げる事務を行うことを業とする。 | |
一 | 登記又は供託に関する手続について代理すること。 |
itemtitleの項目が条の中に複数あるのでループしたいです。
発生している問題・エラーメッセージ
ReferenceError: items is not defined
該当のソースコード
gas
1 2function myFunction() { 3 let url = 'https://elaws.e-gov.go.jp/api/1/lawdata/昭和二十五年法律第百九十七号'; 4 let xml = UrlFetchApp.fetch(url).getContentText(); 5 let document = XmlService.parse(xml); 6 let root = document.getRootElement(); 7 8 const applData = root.getChild("ApplData"); 9 const lawFullText = applData.getChild("LawFullText"); 10 const law = lawFullText.getChild("Law"); 11 const lawBody = law.getChild("LawBody"); 12 const mainProvision = lawBody.getChild("MainProvision"); 13 const chapter = mainProvision.getChild("Chapter"); 14 const articles = chapter.getChildren("Article"); 15 16articles.forEach(function(article){ 17 18 const articleCaption = article.getChild("ArticleCaption"); 19 const articleTitle = article.getChild("ArticleTitle"); 20 const paragraphs = article.getChildren("Paragraph"); 21 22 console.log(articleCaption.getText()); 23 console.log(articleTitle.getText()); 24 25 if (paragraphs != null){ 26 paragraphs.forEach(function(paragraph){ 27 const paragraphNum = paragraph.getChild("ParagraphNum"); 28 const paragraphSentence = paragraph.getChild("ParagraphSentence"); 29 console.log(paragraphNum.getValue()); 30 console.log(paragraphSentence.getValue()); 31 32 const item = paragraph.getChild("Item"); 33 if (item !== null){ 34 items.forEach(function(item){ ●エラーの出ている箇所● 35 const itemtitle = item.getChild("ItemTitle"); 36 const itemSentence = item.getChild("ItemSentence"); 37 const columns = itemSentence.getChild("Column"); 38 console.log(itemtitle.getText()); 39 if (columns !== null){ 40 console.log(itemtitle.getText()); 41 console.log(itemSentence.getValue()); 42 }else{ 43 console.log(columns.getValue()); 44 } 45 }) 46 } 47 }) 48 } 49 }) 50 } 51
試したこと
定義の問題って書いてあったので,
itemをitemsに変えたりしましたがエラーが治りませんでした
paragraphで繰り返し処理ができ、同じようにコードを書きました
XMLの解析がややこしくてすみません。
どなたかご教授いただけないでしょうか。
回答1件
あなたの回答
tips
プレビュー