• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3803
  • Last Modified:

How to get count of XML nodes in PHP using DOMDocument?

Using the snippet below, how can I get the number of FIELD nodes before I iterate through the collection for output?

Many thanks!
$form = new DOMDocument() or die("error");
$form->load("ContactForm.xml");
$fields = $form->getElementsByTagName("Field");
 
// xml...
 
<ContactForm>
	<Field>
		<Label>First Name</Label>
		<Name>contact_first_name</Name>
		<Type>FIELD</Type>
		<Required>1</Required>		
	</Field>
	<Field>
		<Label>Last Name</Label>
		<Name>contact_last_name</Name>
		<Type>FIELD</Type>
		<Required>1</Required>		
	</Field>
        ...
        ...
</ContactForm>

Open in new window

0
redheaded
Asked:
redheaded
1 Solution
 
RurneCommented:
DOMDocument->getElementsByTagName() returns a DOMNodeList.  All DOMNodeLists possess, as a read-only value, a property called "length".

After line 3, you can use `$fields->length` to check how many Field elements were returned.
0
 
redheadedAuthor Commented:
Right on! Thanks!
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now