dongwan5381
2015-07-17 15:58
采纳率: 100%
浏览 41

PHP使用simplexml_load_string加载XML文档

I am new to work with XML files. I've been trying to load the an XML document with the following format as object or array: After the this edit

<?xml version="1.0"?>
<xdm:Device xmlns:xdm="http://www.example.com/schemas/imaging/con/xdm/1.1/" 
           xmlns:dd="http://www.example.com/schemas/imaging/con/dictionaries/1.0/" 
           xmlns:bsi="http://www.example.com/schemas/imaging/con/bsi/2003/08/21" 
           xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
           xmlns:count="http://www.example.com/schemas/imaging/con/counter/2006/01/13" 
           xmlns:media="http://www.example.com/schemas/imaging/con/media/2006/01/13/" 
           xmlns:xsi="http://www.example.org/2001/XMLSchema-instance" 
           xmlns:tt="http://www.example.com/schemas/imaging/con/capabilities/1.1/" 
           xmlns:pwg="http://www.example.com/schemas/imaging/con/pwg/sm/1.0/">
    <xdm:Information>        
        <xdm:Component id="system" componentType="system">
            <dd:MakeAndModel>Samsung LaserJet ML220</dd:MakeAndModel>
            <dd:Description>Blackand while printer</dd:Description>
            <dd:ProductNumber>XB3zzz</dd:ProductNumber>
            <dd:Manufacturer>
                <dd:Name>Samsung</dd:Name>
            </dd:Manufacturer>
        </xdm:Component>
    </xdm:Information>    
</xdm:Device>

I would like to get from every entrance for instance the MakeAndModel, Description, Manufacturer, etc.

I am trying to load the code with the following code currently, but I've tried a lot of things:

$xmlString = file_get_contents('test.xml');
$xml = simplexml_load_string($xmlString); 
var_dump($xml);

I have tried with other simpler XML documents and it works fine. But with my document it does not work, it loads an empty object instead. The file does not have a title. I read in php.net the following in one of the examples:

// The file test.xml contains an XML document with a root element // and at least an element /[root]/title.

How can I achieve load this XML to array or Object?

Edit2:

When I use a simple XML file from example var_dump outputs the following:

[root@windows8 csvmail]# php ppp.php 
object(SimpleXMLElement)#1 (1) {
  ["movie"]=>
  object(SimpleXMLElement)#2 (5) {
    ["title"]=>
    string(22) "PHP: Behind the Parser"

Whit the real file if get the following:

[root@windows8 csvmail]# php ppp.php 
object(SimpleXMLElement)#1 (0) {
}
[root@windows8 csvmail]# 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题