I'm trying to pull xml data from some link withsimplexml_load_file
.
This one xml node I am trying to access is an array with 4 elements.
Each element has a label which I am trying to read.
I try to read each label using theattributes()
function, but for some reason the function only returns the first node label.
// Get data from link
$link = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=12120,34323121&retmode=xml";
$xml = simplexml_load_file($link);
// Try to print attributes:
echo "<pre>";
print_r2($xml->PubmedArticle[1]->MedlineCitation->Article->Abstract->AbstractText->attributes())
echo "</pre>";
Result:
SimpleXMLElement Object
(
[@attributes] => Array
(
[Label] => BACKGROUND
[NlmCategory] => BACKGROUND
)
)
This is the XML from the link. There is a label for each node:BACKGROUND, METHODS, RESULTS, CONCLUSION AND RELEVANCE
<AbstractText Label="BACKGROUND" NlmCategory="BACKGROUND">
Extracorporeal membrane oxygenation (ECMO) is a potential option for the management of severe acute respiratory failure secondary to COVID-19. Conflicting the use of this therapy is the known coagulopathy within COVID-19, leading to an incidence of venous thrombotic events of 25% to 49%. To date, limited guidance is available on optimal anticoagulation strategies in this population.</AbstractText><AbstractText Label="OBJECTIVE" NlmCategory="OBJECTIVE">The purpose of this study was to evaluate the utilization of a pharmacist-driven bivalirudin dosing protocol for anticoagulation in the setting of ECMO for COVID-19-associated respiratory failure.
</AbstractText>
<AbstractText Label="METHODS" NlmCategory="METHODS">
This was a single-center retrospective chart review over a 9-month period of patients receiving bivalirudin while on ECMO. All patients with acute respiratory failure requiring ECMO with a positive SARS-CoV-2 polymerase chain reaction were included. Bivalirudin was dosed via aPTT monitoring after a starting dose of 0.2 or 0.3 mg/kg/h.
</AbstractText>
<AbstractText Label="RESULTS" NlmCategory="RESULTS">
There were 33 patients included in this study, all receiving mechanical ventilation. The most common starting dose of bivalirudin was 0.2 mg/kg/h, with an average time to therapeutic range of 20 hours. Compared to previous reports, rates of bleeding were low at 15.1%, and 6.1% of patients developed a new venous thromboembolic event while on ECMO. ECMO survival was 51.5%, with an ICU mortality rate of 48.5%.
</AbstractText>
<AbstractText Label="CONCLUSION AND RELEVANCE" NlmCategory="UNASSIGNED">
In the first published report of its use within this population, bivalirudin was found to be a viable choice for anticoagulation in those patients on ECMO for severe respiratory failure secondary to COVID-19.
</AbstractText>
Could anyone please tell me why I'm not getting all of the labels from theattributes
function?
SimpleXML::attributes only appears returns the attributes from the first element in the set.
Although undocumented, this is logical, because the attributes are keyed based on the attribute name, and PHP does not allow reuse of the "Label" key in this way. Even if they weren't, I guess it might be hard to distinguish which attributes applied to which elements.
You'll need to rewrite as aforeach
loop or similar.
$labels = [];
foreach ($xml->PubmedArticle[1]->MedlineCitation->Article->Abstract->AbstractText as $t) {
$attributes = $t->attributes();
if (isset($attributes->Label)) {
$labels[] = (string)$attributes->Label;
}
}
var_dump($labels);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
React is currently the leader in JavaScript UI frameworks. First, the Facebook developers started working on this to make their job easier. An app called Facebook Ads grew very quickly, which meant complex management and support. As a result, the team began to create a structure that would help them with efficiency. They had an early prototype before 2011, and two years later, the framework was open source and available to the public. It is currently used by many business giants: AirBNB, PayPal, Netflix, etc.
https://reactjs.org/
Polymer is an open source JavaScript library maintained by Google for building web applications using web components. It also supports both one-way and two-way data binding, creating a broader scope. When you compare Angular to Polymer, since both are built by Google, Angular is a comprehensive framework for developing web applications, whereas Polymer is just a library for developing web components. It was the first library to allow you to create interactive applications using web components. There are currently over 3000 websites using Polymer such as virustotal.com, rogers.com, zeplin.io and so on.
https://polymer-library.polymer-project.org/3.0/docs/devguide/feature-overview
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.