我试图用simplexml从xml文件中提取数据(第一次)。我无法弄清楚如何从属性中获取数据,如下所示:
<field name="http.request.full_uri" showname="Full request URI: http://iof1.idrottonline.se/ImageVaultFiles/id_33176/cf_74/Regler_f-r_beskattning_av_domare.PDF" size="0" pos="58" show="http://iof1.idrottonline.se
如何提取显示属性的值?
以下是我正在使用的xml代码:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="pdml2html.xsl"?>
<!-- You can find pdml2html.xsl in /usr/share/wireshark or at http://anonsvn.wireshark.org/trunk/wireshark/pdml2html.xsl. -->
<pdml version="0" creator="wireshark/1.10.2" time="Mon Mar 31 13:47:36 2014" capture_file="/run/user/1000/gvfs/smb-share:server=10.1.2.2,share=cyberium/Bryan/pcap.2115">
<packet>
<proto name="geninfo" pos="0" showname="General information" size="913">
<field name="num" pos="0" show="7695" showname="Number" value="1e0f" size="913"/>
<field name="len" pos="0" show="913" showname="Frame Length" value="391" size="913"/>
<field name="caplen" pos="0" show="913" showname="Captured Length" value="391" size="913"/>
<field name="timestamp" pos="0" show="Mar 20, 2014 13:24:41.785375000 CET" showname="Captured Time" value="1395318281.785375000" size="913"/>
</proto>
<proto name="frame" showname="Frame 7695: 913 bytes on wire (7304 bits), 913 bytes captured (7304 bits)" size="913" pos="0">
<field name="frame.encap_type" showname="Encapsulation type: Ethernet (1)" size="0" pos="0" show="1"/>
<field name="frame.time" showname="Arrival Time: Mar 20, 2014 13:24:41.785375000 CET" size="0" pos="0" show="Mar 20, 2014 13:24:41.785375000"/>
<field name="frame.offset_shift" showname="Time shift for this packet: 0.000000000 seconds" size="0" pos="0" show="0.000000000"/>
<field name="frame.time_epoch" showname="Epoch Time: 1395318281.785375000 seconds" size="0" pos="0" show="1395318281.785375000"/>
<field name="frame.time_delta" showname="Time delta from previous captured frame: 0.001328000 seconds" size="0" pos="0" show="0.001328000"/>
<field name="frame.time_delta_displayed" showname="Time delta from previous displayed frame: 0.000000000 seconds" size="0" pos="0" show="0.000000000"/>
<field name="frame.time_relative" showname="Time since reference or first frame: 8.913732000 seconds" size="0" pos="0" show="8.913732000"/>
<field name="frame.number" showname="Frame Number: 7695" size="0" pos="0" show="7695"/>
<field name="frame.len" showname="Frame Length: 913 bytes (7304 bits)" size="0" pos="0" show="913"/>
<field name="frame.cap_len" showname="Capture Length: 913 bytes (7304 bits)" size="0" pos="0" show="913"/>
<field name="frame.marked" showname="Frame is marked: False" size="0" pos="0" show="0"/>
<field name="frame.ignored" showname="Frame is ignored: False" size="0" pos="0" show="0"/>
<field name="frame.protocols" showname="Protocols in frame: eth:vlan:ip:tcp:http" size="0" pos="0" show="eth:vlan:ip:tcp:http"/>
<field name="frame.p_prot_data" showname="Number of per-protocol-data: 1" size="0" pos="0" show="1"/>
<field name="" show="[Hypertext Transfer Protocol, key 0]" size="0" pos="0" value=""/>
<field name="frame.coloring_rule.name" showname="Coloring Rule Name: HTTP" size="0" pos="0" show="HTTP"/>
<field name="frame.coloring_rule.string" showname="Coloring Rule String: http || tcp.port == 80" size="0" pos="0" show="http || tcp.port == 80"/>
</proto>
<proto name="eth" showname="Ethernet II, Src: FujitsuT_50:b7:5c (00:19:99:50:b7:5c), Dst: Cisco_53:a0:b1 (00:23:33:53:a0:b1)" size="14" pos="0">
<field name="eth.dst" showname="Destination: Cisco_53:a0:b1 (00:23:33:53:a0:b1)" size="6" pos="0" show="00:23:33:53:a0:b1" value="00233353a0b1">
<field name="eth.addr" showname="Address: Cisco_53:a0:b1 (00:23:33:53:a0:b1)" size="6" pos="0" show="00:23:33:53:a0:b1" value="00233353a0b1"/>
<field name="eth.lg" showname=".... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)" size="3" pos="0" show="0" value="0" unmaskedvalue="002333"/>
<field name="eth.ig" showname=".... ...0 .... .... .... .... = IG bit: Individual address (unicast)" size="3" pos="0" show="0" value="0" unmaskedvalue="002333"/>
</field>
<field name="eth.src" showname="Source: FujitsuT_50:b7:5c (00:19:99:50:b7:5c)" size="6" pos="6" show="00:19:99:50:b7:5c" value="00199950b75c">
<field name="eth.addr" showname="Address: FujitsuT_50:b7:5c (00:19:99:50:b7:5c)" size="6" pos="6" show="00:19:99:50:b7:5c" value="00199950b75c"/>
<field name="eth.lg" showname=".... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)" size="3" pos="6" show="0" value="0" unmaskedvalue="001999"/>
<field name="eth.ig" showname=".... ...0 .... .... .... .... = IG bit: Individual address (unicast)" size="3" pos="6" show="0" value="0" unmaskedvalue="001999"/>
</field>
<field name="eth.type" showname="Type: 802.1Q Virtual LAN (0x8100)" size="2" pos="12" show="0x8100" value="8100"/>
</proto>
<proto name="vlan" showname="802.1Q Virtual LAN, PRI: 0, CFI: 0, ID: 985" size="4" pos="14">
<field name="vlan.priority" showname="000. .... .... .... = Priority: Best Effort (default) (0)" size="2" pos="14" show="0" value="0" unmaskedvalue="03d9"/>
<field name="vlan.cfi" showname="...0 .... .... .... = CFI: Canonical (0)" size="2" pos="14" show="0" value="0" unmaskedvalue="03d9"/>
<field name="vlan.id" showname=".... 0011 1101 1001 = ID: 985" size="2" pos="14" show="985" value="3D9" unmaskedvalue="03d9"/>
<field name="vlan.etype" showname="Type: IP (0x0800)" size="2" pos="16" show="0x0800" value="0800"/>
</proto>
<proto name="ip" showname="Internet Protocol Version 4, Src: 10.1.5.8 (10.1.5.8), Dst: 217.114.81.153 (217.114.81.153)" size="20" pos="18">
<field name="ip.version" showname="Version: 4" size="1" pos="18" show="4" value="45"/>
<field name="ip.hdr_len" showname="Header length: 20 bytes" size="1" pos="18" show="20" value="45"/>
<field name="ip.dsfield" showname="Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00: Not-ECT (Not ECN-Capable Transport))" size="1" pos="19" show="0" value="00">
<field name="ip.dsfield.dscp" showname="0000 00.. = Differentiated Services Codepoint: Default (0x00)" size="1" pos="19" show="0x00" value="0" unmaskedvalue="00"/>
<field name="ip.dsfield.ecn" showname=".... ..00 = Explicit Congestion Notification: Not-ECT (Not ECN-Capable Transport) (0x00)" size="1" pos="19" show="0x00" value="0" unmaskedvalue="00"/>
</field>
<field name="ip.len" showname="Total Length: 895" size="2" pos="20" show="895" value="037f"/>
<field name="ip.id" showname="Identification: 0x03a4 (932)" size="2" pos="22" show="0x03a4" value="03a4"/>
<field name="ip.flags" showname="Flags: 0x02 (Don't Fragment)" size="1" pos="24" show="0x02" value="40">
<field name="ip.flags.rb" showname="0... .... = Reserved bit: Not set" size="1" pos="24" show="0" value="40"/>
<field name="ip.flags.df" showname=".1.. .... = Don't fragment: Set" size="1" pos="24" show="1" value="40"/>
<field name="ip.flags.mf" showname="..0. .... = More fragments: Not set" size="1" pos="24" show="0" value="40"/>
</field>
<field name="ip.frag_offset" showname="Fragment offset: 0" size="2" pos="24" show="0" value="4000"/>
<field name="ip.ttl" showname="Time to live: 128" size="1" pos="26" show="128" value="80"/>
<field name="ip.proto" showname="Protocol: TCP (6)" size="1" pos="27" show="6" value="06"/>
<field name="ip.checksum" showname="Header checksum: 0xb9c0 [correct]" size="2" pos="28" show="0xb9c0" value="b9c0">
<field name="ip.checksum_good" showname="Good: True" size="2" pos="28" show="1" value="b9c0"/>
<field name="ip.checksum_bad" showname="Bad: False" size="2" pos="28" show="0" value="b9c0"/>
</field>
<field name="ip.src" showname="Source: 10.1.5.8 (10.1.5.8)" size="4" pos="30" show="10.1.5.8" value="0a010508"/>
<field name="ip.addr" showname="Source or Destination Address: 10.1.5.8 (10.1.5.8)" hide="yes" size="4" pos="30" show="10.1.5.8" value="0a010508"/>
<field name="ip.src_host" showname="Source Host: 10.1.5.8" hide="yes" size="4" pos="30" show="10.1.5.8" value="0a010508"/>
<field name="ip.host" showname="Source or Destination Host: 10.1.5.8" hide="yes" size="4" pos="30" show="10.1.5.8" value="0a010508"/>
<field name="ip.dst" showname="Destination: 217.114.81.153 (217.114.81.153)" size="4" pos="34" show="217.114.81.153" value="d9725199"/>
<field name="ip.addr" showname="Source or Destination Address: 217.114.81.153 (217.114.81.153)" hide="yes" size="4" pos="34" show="217.114.81.153" value="d9725199"/>
<field name="ip.dst_host" showname="Destination Host: 217.114.81.153" hide="yes" size="4" pos="34" show="217.114.81.153" value="d9725199"/>
<field name="ip.host" showname="Source or Destination Host: 217.114.81.153" hide="yes" size="4" pos="34" show="217.114.81.153" value="d9725199"/>
<field name="" show="Source GeoIP: Unknown" size="4" pos="30" value="0a010508"/>
<field name="" show="Destination GeoIP: Unknown" size="4" pos="34" value="d9725199"/>
</proto>
<proto name="tcp" showname="Transmission Control Protocol, Src Port: 54182 (54182), Dst Port: http (80), Seq: 1, Ack: 1, Len: 855" size="20" pos="38">
<field name="tcp.srcport" showname="Source port: 54182 (54182)" size="2" pos="38" show="54182" value="d3a6"/>
<field name="tcp.dstport" showname="Destination port: http (80)" size="2" pos="40" show="80" value="0050"/>
<field name="tcp.port" showname="Source or Destination Port: 54182" hide="yes" size="2" pos="38" show="54182" value="d3a6"/>
<field name="tcp.port" showname="Source or Destination Port: 80" hide="yes" size="2" pos="40" show="80" value="0050"/>
<field name="tcp.stream" showname="Stream index: 32" size="0" pos="38" show="32"/>
<field name="tcp.len" showname="TCP Segment Len: 855" hide="yes" size="1" pos="50" show="855" value="50"/>
<field name="tcp.seq" showname="Sequence number: 1 (relative sequence number)" size="4" pos="42" show="1" value="e0a940c8"/>
<field name="tcp.nxtseq" showname="Next sequence number: 856 (relative sequence number)" size="0" pos="38" show="856"/>
<field name="tcp.ack" showname="Acknowledgment number: 1 (relative ack number)" size="4" pos="46" show="1" value="de9e6566"/>
<field name="tcp.hdr_len" showname="Header length: 20 bytes" size="1" pos="50" show="20" value="50"/>
<field name="tcp.flags" showname="Flags: 0x018 (PSH, ACK)" size="2" pos="50" show="0x0018" value="18" unmaskedvalue="5018">
<field name="tcp.flags.res" showname="000. .... .... = Reserved: Not set" size="1" pos="50" show="0" value="0" unmaskedvalue="50"/>
<field name="tcp.flags.ns" showname="...0 .... .... = Nonce: Not set" size="1" pos="50" show="0" value="0" unmaskedvalue="50"/>
<field name="tcp.flags.cwr" showname=".... 0... .... = Congestion Window Reduced (CWR): Not set" size="1" pos="51" show="0" value="0" unmaskedvalue="18"/>
<field name="tcp.flags.ecn" showname=".... .0.. .... = ECN-Echo: Not set" size="1" pos="51" show="0" value="0" unmaskedvalue="18"/>
<field name="tcp.flags.urg" showname=".... ..0. .... = Urgent: Not set" size="1" pos="51" show="0" value="0" unmaskedvalue="18"/>
<field name="tcp.flags.ack" showname=".... ...1 .... = Acknowledgment: Set" size="1" pos="51" show="1" value="1" unmaskedvalue="18"/>
<field name="tcp.flags.push" showname=".... .... 1... = Push: Set" size="1" pos="51" show="1" value="1" unmaskedvalue="18"/>
<field name="tcp.flags.reset" showname=".... .... .0.. = Reset: Not set" size="1" pos="51" show="0" value="0" unmaskedvalue="18"/>
<field name="tcp.flags.syn" showname=".... .... ..0. = Syn: Not set" size="1" pos="51" show="0" value="0" unmaskedvalue="18"/>
<field name="tcp.flags.fin" showname=".... .... ...0 = Fin: Not set" size="1" pos="51" show="0" value="0" unmaskedvalue="18"/>
</field>
<field name="tcp.window_size_value" showname="Window size value: 258" size="2" pos="52" show="258" value="0102"/>
<field name="tcp.window_size" showname="Calculated window size: 66048" size="2" pos="52" show="66048" value="0102"/>
<field name="tcp.window_size_scalefactor" showname="Window size scaling factor: 256" size="2" pos="52" show="256" value="0102"/>
<field name="tcp.checksum" showname="Checksum: 0x028e [validation disabled]" size="2" pos="54" show="0x028e" value="028e">
<field name="tcp.checksum_good" showname="Good Checksum: False" size="2" pos="54" show="0" value="028e"/>
<field name="tcp.checksum_bad" showname="Bad Checksum: False" size="2" pos="54" show="0" value="028e"/>
</field>
<field name="tcp.analysis" showname="SEQ/ACK analysis" size="0" pos="38" show="" value="">
<field name="tcp.analysis.bytes_in_flight" showname="Bytes in flight: 855" size="0" pos="38" show="855"/>
</field>
</proto>
<proto name="http" showname="Hypertext Transfer Protocol" size="855" pos="58">
<field name="" show="GET /ImageVaultFiles/id_33176/cf_74/Regler_f-r_beskattning_av_domare.PDF HTTP/1.1\r\n" size="83" pos="58" value="474554202f496d6167655661756c7446696c65732f69645f33333137362f63665f37342f5265676c65725f662d725f6265736b6174746e696e675f61765f646f6d6172652e50444620485454502f312e310d0a">
<field name="expert" showname="Expert Info (Chat/Sequence): GET /ImageVaultFiles/id_33176/cf_74/Regler_f-r_beskattning_av_domare.PDF HTTP/1.1\r\n" size="0" pos="58">
<field name="expert.message" showname="Message: GET /ImageVaultFiles/id_33176/cf_74/Regler_f-r_beskattning_av_domare.PDF HTTP/1.1\r\n" size="0" pos="0" show="GET /ImageVaultFiles/id_33176/cf_74/Regler_f-r_beskattning_av_domare.PDF HTTP/1.1\\r\\n"/>
<field name="expert.severity" showname="Severity level: Chat" size="0" pos="0" show="0x00200000"/>
<field name="expert.group" showname="Group: Sequence" size="0" pos="0" show="0x02000000"/>
</field>
<field name="http.request.method" showname="Request Method: GET" size="3" pos="58" show="GET" value="474554"/>
<field name="http.request.uri" showname="Request URI: /ImageVaultFiles/id_33176/cf_74/Regler_f-r_beskattning_av_domare.PDF" size="68" pos="62" show="/ImageVaultFiles/id_33176/cf_74/Regler_f-r_beskattning_av_domare.PDF" value="2f496d6167655661756c7446696c65732f69645f33333137362f63665f37342f5265676c65725f662d725f6265736b6174746e696e675f61765f646f6d6172652e504446"/>
<field name="http.request.version" showname="Request Version: HTTP/1.1" size="8" pos="131" show="HTTP/1.1" value="485454502f312e31"/>
</field>
<field name="http.host" showname="Host: iof1.idrottonline.se\r\n" size="28" pos="141" show="iof1.idrottonline.se" value="486f73743a20696f66312e6964726f74746f6e6c696e652e73650d0a"/>
<field name="http.connection" showname="Connection: keep-alive\r\n" size="24" pos="169" show="keep-alive" value="436f6e6e656374696f6e3a206b6565702d616c6976650d0a"/>
<field name="http.accept" showname="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" size="84" pos="193" show="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" value="4163636570743a20746578742f68746d6c2c6170706c69636174696f6e2f7868746d6c2b786d6c2c6170706c69636174696f6e2f786d6c3b713d302e392c696d6167652f776562702c2a2f2a3b713d302e380d0a"/>
<field name="http.user_agent" showname="User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36\r\n" size="123" pos="277" show="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36" value="557365722d4167656e743a204d6f7a696c6c612f352e30202857696e646f7773204e5420362e313b20574f57363429204170706c655765624b69742f3533372e333620284b48544d4c2c206c696b65204765636b6f29204368726f6d652f33332e302e313735302e313534205361666172692f3533372e33360d0a"/>
<field name="http.referer" showname="Referer: https://www.google.se/\r\n" size="33" pos="400" show="https://www.google.se/" value="526566657265723a2068747470733a2f2f7777772e676f6f676c652e73652f0d0a"/>
<field name="http.accept_encoding" showname="Accept-Encoding: gzip,deflate,sdch\r\n" size="36" pos="433" show="gzip,deflate,sdch" value="4163636570742d456e636f64696e673a20677a69702c6465666c6174652c736463680d0a"/>
<field name="http.accept_language" showname="Accept-Language: en-US,en;q=0.8,sv;q=0.6\r\n" size="42" pos="469" show="en-US,en;q=0.8,sv;q=0.6" value="4163636570742d4c616e67756167653a20656e2d55532c656e3b713d302e382c73763b713d302e360d0a"/>
<field name="http.cookie" showname="[truncated] Cookie: __atssc=facebook%3B9; __utma=98271239.120658342.1382003889.1393920120.1394109484.41; __utmz=98271239.1393248832.36.9.utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utma=130276567.1675894337.1382003889" size="400" pos="511" show="__atssc=facebook%3B9; __utma=98271239.120658342.1382003889.1393920120.1394109484.41; __utmz=98271239.1393248832.36.9.utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utma=130276567.1675894337.1382003889.1393920120.1394109484.41; __utmz=130276567.1393248832.36.9.utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __atuvc=3%7C6%2C9%7C7%2C15%7C8%2C3%7C9%2C3%7C10" value="436f6f6b69653a205f5f61747373633d66616365626f6f6b253342393b205f5f75746d613d39383237313233392e3132303635383334322e313338323030333838392e313339333932303132302e313339343130393438342e34313b205f5f75746d7a3d39383237313233392e313339333234383833322e33362e392e75746d6373723d66616365626f6f6b2e636f6d7c75746d63636e3d28726566657272616c297c75746d636d643d726566657272616c7c75746d6363743d2f3b205f5f75746d613d3133303237363536372e313637353839343333372e313338323030333838392e313339333932303132302e313339343130393438342e34313b205f5f75746d7a3d3133303237363536372e313339333234383833322e33362e392e75746d6373723d66616365626f6f6b2e636f6d7c75746d63636e3d28726566657272616c297c75746d636d643d726566657272616c7c75746d6363743d2f3b205f5f61747576633d3325374336253243392537433725324331352537433825324333253743392532433325374331300d0a"/>
<field name="" show="\r\n" size="2" pos="911" value="0d0a"/>
<field name="http.request.full_uri" showname="Full request URI: http://iof1.idrottonline.se/ImageVaultFiles/id_33176/cf_74/Regler_f-r_beskattning_av_domare.PDF" size="0" pos="58" show="http://iof1.idrottonline.se/ImageVaultFiles/id_33176/cf_74/Regler_f-r_beskattning_av_domare.PDF"/>
<field name="http.request" showname="Request: True" hide="yes" size="0" pos="58" show="1"/>
<field name="" show="HTTP request 1/2" size="0" pos="58" value=""/>
<field name="http.response_in" showname="Response in frame: 8046" size="0" pos="58" show="8046"/>
<field name="http.next_request_in" showname="Next request in frame: 8062" size="0" pos="58" show="8062"/>
</proto>
</packet>
答案 0 :(得分:0)
您可以尝试以下代码。
$xml = new SimpleXMLElement($xmlString);
$source = $xml->packet->proto->field->attributes()->name;
这只是获取属性的示例。你可以试试。
答案 1 :(得分:0)
来自PHP文档:
<?php
$string = <<<XML
<a>
<foo name="one" game="lonely">1</foo>
</a>
XML;
$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>
因此,您可以使用简单的foreach循环遍历所有属性。如果您只想获得单个属性,请使用:
$xml->your_full_path_goes_here->field->attributes()->attribute_you_want;
祝你好运!
编辑:再读一遍,你的完整路径应该是:
$xml->packet->proto[6]->field[10]->attributes()->showname;
)