简体   繁体   中英

How to get values from XML

I'm using PHP. I am novice at XML. I want to set all the values as variables to use elsewhere.

For example, <property name="HoldType" value="4" /> I want to set $holdtype_value to equal 4.

Further complication; <property class="Action0"> has a child property with name="Class", so does the Action1 property. I need a variable for those values.

Here is an example item from the XML. Some items have more or less properties.

    <item id="24" name="hoe">
    <property name="Meshfile" value="Items/Tools/hoe_iron" />
    <property name="Material" value="metal" />
    <property name="HoldType" value="4" />
    <property name="Stacknumber" value="1" />
    <property name="RepairTools" value="forgedIron" />
    <property name="Degradation" value="300" param1="true" />
    <property name="SoundDestroy" value="wooddestroy1" />
    <property name="FuelValue" value="24" />
    <property name="Weight" value="32" />
    <property class="Action0">
        <!-- AttackAction -->
        <property name="Class" value="Melee" />
        <property name="Delay" value="2.1" />
        <property name="Range" value="2" />
        <property name="Sphere" value="0.2" />
        <property name="Block_range" value="4" />
        <property name="DamageEntity" value="5" />
        <property name="DamageBlock" value="1" />
        <property name="Sound_start" value="swoosh" />
        <property name="Stamina_usage" value="8" />
        <property name="DamageBonus.earth" value="2" />
        <property name="DamageBonus.glass" value="25" />
        <property name="DamageBonus.head" value="4" />
    </property>
    <property class="Action1">
        <!-- UseAction -->
        <property name="Class" value="MakeFertile" />
        <property name="Delay" value="2.1" />
        <property name="Block_range" value="4" />
        <property name="DamageBlock" value="1" />
        <property name="Sound_start" value="swoosh" />
        <property name="Sound_end" value="UseActions/repair_block" />
        <property name="Fertileblock" value="fertileFarmland" />
        <property name="Adjacentblock" value="dirt" />
    </property>
    <property name="Group" value="Tools/Traps" />
    <property class="Preview">
        <property name="Zoom" value="0" />
        <property name="Pos" value="0,0" />
    </property>
    <property name="ActionSkillGroup" value="Mining Tools"/>
    <property name="CraftingSkillGroup" value="Tool Smithing"/>
</item>

You need to investigate using the DOM , specifically DOMDocument::loadXML() here: http://php.net/manual/en/domdocument.loadxml.php

This will allow you to load the doc and then traverse it in a variety of ways, setting elements (nodes), attributes, etc. Quick example from the docs for loading an XML string...

$doc = new DOMDocument();
$doc->loadXML('<root><node/></root>');
/* ... traverse the document here ... do your stuff ... */
echo $doc->saveXML();

There are tons of help docs, tuts, etc. surrounding DOM support in PHP.

EDIT: The above is good for 95% of the XML you'll run into out there, but if you're loading LARGE files worth of XML, you'll want ot look at PHP's SimpleXML instead. DOM loads the entire doc at once, whereas, SimpleXML allows you to traverse a document one line at a time. This means no memory issues on large files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM