【发布时间】:2015-04-23 09:47:16
【问题描述】:
我需要访问 xml 中的所有子元素,如下所示。每个不为空的元素,我都想拥有它的价值和路径。这样的事情可能吗?我可以在 xml::simple 中编写手动解析,但感觉不对。
<Incident>
<Organisation>
<USD_Local>Secure Cloud Container</USD_Local>
</Organisation>
<Location/>
<Contact_Reference>
<Organisation>
<USD_Local>Secure Cloud Container</USD_Local>
</Organisation>
<Contact_ID/>
<Contact_Name>
<First_Name/>
<Last_Name/>
</Contact_Name>
<External_Reference>AMZZEMP:000000000152299</External_Reference>
<Freeform_Details/>
<Email_Address/>
</Contact_Reference>
<Requested_By>
<Organisation>
<USD_Local>Secure Cloud Container</USD_Local>
</Organisation>
<Contact_ID/>
<Contact_Name>
<First_Name/>
<Last_Name/>
</Contact_Name>
<External_Reference>AMZZEMP:000000000152299</External_Reference>
<Freeform_Details/>
<Email_Address/>
<Additional_Attribute>Security-AAHPS.Other</Additional_Attribute>
</Requested_By>
<Incident_Type>Incident</Incident_Type>
<Severity>2</Severity>
<Category>
<USD_Local>Security-AAHPS.Other</USD_Local>
<USD_Foreign/>
</Category>
<Service_Type>
<Name/>
<Target_Duration/>
</Service_Type>
<Group>
<USD_Local>UK.Security.SOC</USD_Local>
</Group>
<Configuration_Item action="Add_Link">
<Name>CATPLD2PRSSEN03</Name>
<CI_Number/>
<Serial_Number/>
<System_Name/>
<External_Reference/>
<Additional_Attribute/>
</Configuration_Item>
<Summary>Topbanana</Summary>
<Customer_Reference>SCC</Customer_Reference>
<Supplier_Reference/>
<Occurred_Date_Time format="yyyy/MM/dd HH:mm:ss">2015/04/01 10:50:52</Occurred_Date_Time>
</Incident>
【问题讨论】:
-
你使用什么模块? XML::Twig 或 XML::LibXML?
-
对于以前的 xml,我使用了 XML::LibXML 和 XML::Simple ...但手动提取元素感觉不对...我也使用 XML::XPath、XML::XPath: .XMLParser
-
"每一个不为空的元素,我都想要它的值和路径。这样的事情可能吗?" 是的,这是可能的。请发布您期望获得的确切代码。如果您尝试过任何事情,也请发布。如果你没有,为什么不呢?
-
除了使用 xml::simple 手动解析之外,我还没有写任何东西。不是很好,我只是在研究选项……我的 $parser = new XML::Simple; #parse 响应:我的 $xml = $parser->XMLin($file); $xml->{Control_Data}->{Document_Source};
-
我们需要查看您的预期输出(XML?HTML 表格?文本?)。 “不为空的元素”也是模棱两可的:您的示例中的
<Contact_Name>是“空的”吗?它包含其他(空)元素。
标签: xml perl parsing xslt xpath