1

I have an XML string that I just want to grab one variable in a foreach, but I can't figure out how to do that from my subpar Googling skills.

Sample XML:

<DocuSignEnvelopeInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.docusign.net/API/3.0">
<EnvelopeStatus>
<RecipientStatuses>
<RecipientStatus>
<Type>Signer</Type>
<Email>[omitted]</Email>
<UserName>[omitted]</UserName>
<RoutingOrder>1</RoutingOrder>
<Sent>2014-06-30T19:21:47.633</Sent>
<DeclineReason xsi:nil="true"/>
<Status>Sent</Status>
<RecipientIPAddress/>
<CustomFields/>
<TabStatuses>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>85</XPosition>
<YPosition>147</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_1</TabLabel>
<TabName>Account Title</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>1</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>402</XPosition>
<YPosition>147</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_53</TabLabel>
<TabName>Second Account Title - skip if not applicable.</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>1</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>83</XPosition>
<YPosition>981</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_34</TabLabel>
<TabName>Print Primary Client Name</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>83</XPosition>
<YPosition>1250</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_48</TabLabel>
<TabName>
Required for all except foreign persons: Social Security or Taxpayer ID # - check appropriate box
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern>^\d{3}-\d{2}-\d{4}$|^\d{2}-\d{7}$</ValidationPattern>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>83</XPosition>
<YPosition>1514</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_51</TabLabel>
<TabName>Print Primary Client Name</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>77</XPosition>
<YPosition>1125</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_38</TabLabel>
<TabName>
Required: Check 1 Federal Tax Classification - Individual/Sole Proprietor
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>314</XPosition>
<YPosition>1127</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_39</TabLabel>
<TabName>
Required: Check 1 Federal Tax Classification - C Corporation
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>485</XPosition>
<YPosition>1127</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_40</TabLabel>
<TabName>
Required: Check 1 Federal Tax Classification - S Corporation
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>652</XPosition>
<YPosition>1127</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_41</TabLabel>
<TabName>
Required: Check 1 Federal Tax Classification - Partnership
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>800</XPosition>
<YPosition>1127</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_42</TabLabel>
<TabName>
Required: Check 1 Federal Tax Classification - Trust / Estate
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>SignHere</TabType>
<Status>Active</Status>
<XPosition>641</XPosition>
<YPosition>920</YPosition>
<TabLabel>Signature 30</TabLabel>
<TabName>Sign Here</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
</TabStatus>
<TabStatus>
<TabType>DateSigned</TabType>
<Status>Active</Status>
<XPosition>1016</XPosition>
<YPosition>975</YPosition>
<TabLabel>Date Signed</TabLabel>
<TabName>Date Signed</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
</TabStatus>
<TabStatus>
<TabType>SignHere</TabType>
<Status>Active</Status>
<XPosition>641</XPosition>
<YPosition>1456</YPosition>
<TabLabel>Signature 34</TabLabel>
<TabName>Sign Here</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
</TabStatus>
<TabStatus>
<TabType>DateSigned</TabType>
<Status>Active</Status>
<XPosition>1016</XPosition>
<YPosition>1508</YPosition>
<TabLabel>Date Signed</TabLabel>
<TabName>Date Signed</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>545</XPosition>
<YPosition>450</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_30</TabLabel>
<TabName>
Preferred User ID (if available - 15 characters maximum) - Provide if "Yes" checked.
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>302</XPosition>
<YPosition>475</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_31</TabLabel>
<TabName>Client E-Mail Address - Provide if "Yes" checked.</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>939</XPosition>
<YPosition>475</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_32</TabLabel>
<TabName>Mother's Maiden Name - Provide if "Yes" checked.</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>720</XPosition>
<YPosition>500</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_33</TabLabel>
<TabName>4-Digit PIN Number - Provide if "Yes" checked.</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>908</XPosition>
<YPosition>1145</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_44</TabLabel>
<TabName>
Required for LLC: Enter C = C Corporation, S = S Corporation, or P = Partnership - 1 character max.
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>154</XPosition>
<YPosition>1168</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_46</TabLabel>
<TabName>
Required to describe Federal Tax Classification if "Other" is checked.
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>439</XPosition>
<YPosition>1252</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_49</TabLabel>
<TabName>
Required for all except foreign persons to check either SSN or TIN
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>754</XPosition>
<YPosition>1252</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_50</TabLabel>
<TabName>
Required for all except foreign persons to check either SSN or TIN
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>77</XPosition>
<YPosition>1145</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_43</TabLabel>
<TabName>
Required: Check 1 Federal Tax Classification - LLC. Next field required if LLC is checked.
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>75</XPosition>
<YPosition>1168</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_45</TabLabel>
<TabName>
None of the above - Required to describe in next field.
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>81</XPosition>
<YPosition>431</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_28</TabLabel>
<TabName>
YES, I want to receive electronic delivery of Account Communications - Fill fields in Section IV.
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>712</XPosition>
<YPosition>431</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_29</TabLabel>
<TabName>
NO, I do not want electronic delivery - Please skip Section IV, E-Delivery Election.
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Checkbox</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>731</XPosition>
<YPosition>147</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_3</TabLabel>
<TabName>For Branch Office Use Only.</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>1</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>970</XPosition>
<YPosition>147</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_27</TabLabel>
<TabName>For Branch Office Use Only.</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>1</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>1010</XPosition>
<YPosition>1125</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_57</TabLabel>
<TabName>
Exempt Payee Code (if any) - If unknown, please contact advisor.
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
<TabStatus>
<TabType>Custom</TabType>
<Status>Active</Status>
<XPosition>1120</XPosition>
<YPosition>1166</YPosition>
<TabLabel>{4CFA794D-5D17-4196-8BF1-0044FCE8AC03}_58</TabLabel>
<TabName>
Exemption from FATCA reporting code (if any) - If unknown, please contact advisor.
</TabName>
<TabValue/>
<DocumentID>1</DocumentID>
<PageNumber>2</PageNumber>
<ValidationPattern/>
<CustomTabType>Text</CustomTabType>
</TabStatus>
</TabStatuses>
<AccountStatus>Active</AccountStatus>
<RecipientId>664a68e7-63f0-4731-94eb-84d6c6d473e2</RecipientId>
</RecipientStatus>
</RecipientStatuses>
<TimeGenerated>2014-06-30T19:22:08.2579681</TimeGenerated>
<EnvelopeID>StackOverflow</EnvelopeID>
<Subject>[omitted]</Subject>
<UserName>[omitted]</UserName>
<Email>[omitted]</Email>
<Status>Sent</Status>
<Created>2014-06-30T19:21:47.26</Created>
<Sent>2014-06-30T19:21:47.683</Sent>
<ACStatus>Original</ACStatus>
<ACStatusDate>2014-06-30T19:21:47.26</ACStatusDate>
<ACHolder>[omitted]</ACHolder>
<ACHolderEmail>[omitted]</ACHolderEmail>
<ACHolderLocation>DocuSign</ACHolderLocation>
<SigningLocation>Online</SigningLocation>
<SenderIPAddress>54.213.82.153</SenderIPAddress>
<EnvelopePDFHash/>
<CustomFields>
<CustomField>
<Name>[omitted]</Name>
<Show>True</Show>
<Required>False</Required>
<Value>BETA v0.0.8.2</Value>
</CustomField>
</CustomFields>
<AutoNavigation>true</AutoNavigation>
<EnvelopeIdStamping>true</EnvelopeIdStamping>
<AuthoritativeCopy>false</AuthoritativeCopy>
<DocumentStatuses>
<DocumentStatus>
<ID>1</ID>
<Name>
Account Agreement and W-9 Request for Taxpayer ID.pdf
</Name>
<TemplateName>Account Agreement and W-9 Request for Taxpayer ID</TemplateName>
<Sequence>1</Sequence>
</DocumentStatus>
</DocumentStatuses>
</EnvelopeStatus>
<DocumentPDFs>
<DocumentPDF>
<Name>
Account Agreement and W-9 Request for Taxpayer ID.pdf
</Name>
<DocumentType>CONTENT</DocumentType>
<PDFBytes>qoiwehfpqoiwhefpoqwihepoqwihefpaefhapwe</PDFBytes>
</DocumentPDF>
</DocumentPDFs>
</DocuSignEnvelopeInformation>

Here is my code that gives all of the data of the nodes

  $xmlDoc = new DOMDocument();
  $xmlDoc->loadXML($str);
  $xmlDoc->formatOutput=true;
  $spanString = htmlentities($xmlDoc->saveXML());
  $test = $xmlDoc->getElementsByTagName('DocumentPDF');

  foreach ($test as $stuff) {
    echo $stuff->nodeValue . "<br>";
  }

output:

Account Agreement and W-9 Request for Taxpayer ID.pdfCONTENTqoiwehfpqoiwhefpoqwihepoqwihefpaefhapwe

I don't have a lot of experience with XML manipulation in PHP. I'd assume if I changed echo $stuff->nodeValue . "<br>"; to echo $stuff->getElementsByTagName('Name')->nodeValue . "<br>"; that it would work. However it gives an error Undefined property: DOMNodeList::$nodeValue

What I want to output:

Account Agreement and W-9 Request for Taxpayer ID.pdf

2 Answers 2

2

You can use XPath but since the document uses namespaces, you'll need to register the document namespace to be able to obtain the elements:

$doc = new DOMDocument();
$doc->load('so.xml');

$selector = new DOMXPath($doc);

# Register the document namespace !
$selector->registerNamespace('docusign', 'http://www.docusign.net/API/3.0');

foreach($selector->query('//docusign:DocumentPDF/docusign:Name') as $element) {
    echo $element->nodeValue . '<br/>';
}
Sign up to request clarification or add additional context in comments.

7 Comments

In this case the 'morestuff' is actually 'Name' which shows up multiple times in this XML (hundreds of thousands of tags). I just want this specific set of tag's Name value
I'm not getting a response with that Query... here's my var_dump of $elements: object(DOMNodeList)#3 (1) { ["length"]=> int(0) }
Can you show the real xml? I think we are talking not about the same data. Otherwise the query should work
connect.cuddledog.com/XML/StackOverflow.xml so I'm looking at DocumentPDF -> Name
test = DocumentPDF and morestuff = Name
|
0

try:

$test = $xmlDoc->getElementsByTagName('test');

  foreach ($test->childNodes as $stuff) {
    if($stuff->nodeName=='morestuff')  {
      echo $stuff->nodeValue . "<br>";
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.