我有一个XML字符串,我只想在foreach中获取一个变量,但我无法弄清楚如何从我的subpar Google搜索技能中做到这一点。
示例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>
这是我的代码,它提供了节点的所有数据
$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>";
}
输出:
Account Agreement and W-9 Request for Taxpayer ID.pdfCONTENTqoiwehfpqoiwhefpoqwihepoqwihefpaefhapwe
我在PHP中没有很多XML操作经验。我假设我将echo $stuff->nodeValue . "<br>";
更改为echo $stuff->getElementsByTagName('Name')->nodeValue . "<br>";
它会起作用。
但是它会出现错误Undefined property: DOMNodeList::$nodeValue
我想输出的内容:
Account Agreement and W-9 Request for Taxpayer ID.pdf
答案 0 :(得分:2)
您可以使用XPath
但由于文档使用名称空间,您需要注册文档名称空间才能获取元素:
$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/>';
}
答案 1 :(得分:0)
尝试:
$test = $xmlDoc->getElementsByTagName('test');
foreach ($test->childNodes as $stuff) {
if($stuff->nodeName=='morestuff') {
echo $stuff->nodeValue . "<br>";
}
}