如何将可重复的xml子标记导入一个mysql列

jgwigjjp  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(303)

我无法将xml中的子标记聚合到mysql中的同一列中。

<content>
<Article>
<ArticleTitle>Idque Caesaris facere voluntate liceret: sese habere.</ArticleTitle>
<Abstract>
<AbstractText Label="INTRODUCTION" NlmCategory="BACKGROUND">Phasellus laoreet.</AbstractText>
<AbstractText Label="METHODS" NlmCategory="METHODS">Plura mihi bona.</AbstractText>
<AbstractText Label="RESULTS" NlmCategory="RESULTS">Curabitur blandit.</AbstractText>
<AbstractText Label="CONCLUSIONS" NlmCategory="CONCLUSIONS">Morbi fringilla convallis sapien, id pulvinar odio volutpat.</AbstractText>
<CopyrightInformation>Copyright © 2018.</CopyrightInformation>
</Abstract>
<AuthorList>
<Author >
<LastName>Smith</LastName>
<ForeName>John</ForeName>
<Initials>JS</Initials>
</Author>
<Author >
<LastName>Brown</LastName>
<ForeName>Jake</ForeName>
<Initials>JB</Initials>
</Author>
<AuthorList>
</Article>

<Article>
<ArticleTitle>Contra legem facit qui id facit quod lex prohibet.                        
</ArticleTitle>
<Abstract>
<AbstractText>Phasellus laoreet.</AbstractText>
</Abstract>
<AuthorList>
<Author>
<LastName>Bond</LastName>
<ForeName>James</ForeName>
<Initials>JB</Initials>
</Author>
<Author >
<LastName>Bourne</LastName>
<ForeName>Jason</ForeName>
<Initials>JB</Initials>
</Author>
</AuthorList>
</Article>
</content>

我的php代码:

$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$url = 'scripts/testy.xml';
$xml = file_get_contents($url, false, $context);
$xml = simplexml_load_string($xml) or die("ERROR: Cannot create SimpleXML object");
foreach ($xml->content as $item) {
$title =  $item->Article->ArticleTitle;`enter code here`

foreach($item->Article->Abstract->children() as $textNode) {
$abstract_text=$textNode;
}
$sql = "INSERT INTO testy (title, abstract)
VALUES (:title, :abstract)";

$stmt = $conn->prepare($sql);
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':abstract', $abstract_text, PDO::PARAM_STR);
$stmt->execute();
 }

现在“标题”很好用,抽象导入很好,只有一个标签-对于更多这样的标签-导入只是最后一个。预期的mysql表包含所有“abstracttext”和“copyrightinformation”标记的聚合数据,以及“jame bond,jason bourne”等下一列autor。

sr4lhrrt

sr4lhrrt1#

你不想连接这个字符串吗?注意 .= .

$abstract_text= "";
foreach($item->Article->Abstract->children() as $textNode) {
    $abstract_text .= $textNode;
}

相关问题