0

My XML:

<WorkingTime>
    <FromTime>08:00</FromTime>
    <ToTime>11:00</ToTime>
    <Name>Izpit</Name>
    <Owner>Marko</Owner>
    <Category>
        <School Professor="111" Room="1" Subject="882" />
    </Category>
</WorkingTime>

<Professors>
    <Professor email="xxx" id="111" code="String">Name 1</Professor>
    <Professor email="xxx" id="222" code="String">Name 2</Professor>
    <Professor email="xxx" id="333" code="String">Name 3</Professor>
</Professors>
<Rooms>
    <Room id="1">IA-301</Room>
    <Room id="2">A-302</Room>
    <Room id="3">A-303</Room>
    <Room id="4">A-304</Room>
    <Room id="5">A-305</Room>
    <Room id="6">A-306</Room>

</Rooms>
<Subjects>
    <Subject id="881">Vaje</Subject>
    <Subject id="882">Kolokvij</Subject>
    <Subject id="883">Predmet</Subject>
    <Subject id="884">Izpit</Subject>
</Subjects>

How can I in php now print Professor.

I can get id like:

$professor = $workingTime->Category->School->attributes()->Professor;
$room      = $workingTime->Category->School->attributes()->Room;
$subject   = $workingTime->Category->School->attributes()->Subject;

but how to now get value:

I try this but is empty:

echo $xml->Professors->Professor[$professor];

And how can I update values.

I want to save:

<Category>
    <School Professor="222" Room="2" Subject="883" />
</Category>

I am using dom_import_simplexml and simplexml_load_file.

1 Answer 1

2

Professor is an element, not an attribute. Therefore using attributes() to access that element makes no sense, does not work and - as you clearly write already in your question - gets you an "empty" element.

On the other hand, id is an attribute and could be accessed that way.

However for both cases I would suggest you to follow the description given in the base SimpleXML examples in the PHP manual which are a good entry level tutorial:

Here on Stackoverflow, you need to search for the concrete problems btw. not ask about all problems alltogether, e.g. Q&A material like:

I hope this helps you obtaining the information you're looking for. Let me know if you find better Q&A here on site regarding the subjects you need to cover and as well if you're missing something.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.