簡體   English   中英

獲取XML標記和屬性直至n級

[英]Get XML tag and attribute upto n level

是否可以獲取標簽名稱和屬性名稱的值最多為n級。 例如,

文件1:

<?xml ?>
<data>
    <student>
        <std>4th</std>
        <student>Alex</student>
        <division name="A" />
    </student>
</data>

檔案2:

<?xml ?>
<data>
    <students>
        <student>
            <std>4th</std>
            <student>Alex</student>
            <division name="A" />
        </student>
        <student>
            <std>5th</std>
            <student>Alice</student>
            <division name="D" />
            <subject>Drawing</subject>
        </student>
    </students>
</data>

文件3:

<?xml ?>
<data>
    <students>
        <student>
            <std>4th</std>
            <student>Alex</student>
            <division name="A" />
        </student>
        <student>
            <std>5th</std>
            <student>Alice</student>
            <division name="D" />
            <subject>Drawing</subject>
        </student>
    </students>
    <students>
        <student>
            <std>4th</std>
            <student>Alex</student>
        </student>
        <student>
            <std>5th</std>
            <student>Alice</student>
            <division name="D" />
            <subject>Drawing</subject>
        </student>
        <student>
            <std>5th</std>
            <student>Alice</student>
            <division name="D" />
            <subject>
                <sub1>Drwaing</sub1>
                <sub1>Maths</sub1>
            </subject>
        </student>
    </students>
</data>

上面是示例文件1是簡單文件,文件2具有更多標簽,文件3比其他文件更長。 所以我的基本問題是XML文件格式不是預定義的,可能就像上面的示例一樣。 那么是否有可能在PHP中獲取標簽名稱,屬性名稱及其值? 如果是,那怎么辦?

我已經用$xml->children()->children()$child->getName()嘗試了一些解決方案, $xml->children()->children()我找不到任何完美的方法,通常它適合2級或3級,但沒有N級解決方案xml標記。

我嘗試如下:

$xml='<person>
    <child role="son">
        <child role="daughter"/>
    </child>
    <child role="daughter">
        <child role="son">
            <child role="son"/>
            <name role="trainer">
                <nickname role="assistant">Nic</nickname>
                <fname>Nicolas</fname>
                <lname>Johnson</lname>
                <hobby>
                    <sport>
                        <game>
                            <name>Cricket</name>
                            <name type="male">Hockey</name>
                            <name type="national" />
                        </game>
                    </sport>
                </hobby>
            </name>
        </child>
    </child>
    <child role="daughter">
        <child role="son">
            <child role="son"/>
            <name role="student">Jessika</name>
        </child>
        <child role="son">
            <child role="son"/>
            <names>
                <name>Nick</name>
            </names>
        </child>
    </child>
    <child role="daughter">
        <child role="son">
            <child role="son"/>
        </child>
        <child role="son">
            <child role="son"/>
            <child role="son"/>
            <name>Tom</name>
        </child>
        <child role="son">
            <child role="son"/>
            <child role="son"/>
            <name>John</name>
        </child>
    </child>
</person>';
$xml = new SimpleXMLElement($xml);
foreach ($xml->children() as $second_gen) {
    echo '<br> role=' . $second_gen['role'];
    foreach ($second_gen->children() as $third_gen) {
        echo '<br> role=' . $third_gen['role'];
        foreach ($third_gen->children() as $fourth_gen) {
            echo '<br> role=' . $third_gen['role'];
        }
    }
}

請注意,file1,file2和file3的示例不同,並且上面的示例也不同,所以請不要混淆,我已經提到XML文件和格式不是預定義的,它可以是任何級別。

在上面的示例中,我嘗試了3個foreach循環,但是當達到N級時,它無法正常工作。 我想完全動態地做。 可能嗎?

如果您有更好的想法或方法,可以分享。 感謝您的寶貴時間。

我想做以下。

我想從XML獲取一些值,例如學生姓名,費用,標准,流,科目,部門和其他常規詳細信息。 現在的主要問題是XML格式未預定義,可能就像我提到的上述示例一樣。 它是完全動態的,我想根據標簽和/或屬性值存儲在表中。

如果有任何疑問,可以隨時詢問,我希望能對我想說的有用。 謝謝。

Xpath表達式使您可以從XML文檔中獲取節點(和值)。

$xml = <<<'XML'
<data>
    <student>
        <std>4th</std>
        <student>Alex</student>
        <division name="A" />
    </student>
</data>
XML;

$data = new SimpleXMLElement($xml);

foreach($data->xpath('//student[student]') as $student) {
  var_dump(
    [
      (string)$student->std,
      (string)$student->student,
      (string)$student->division['name']
    ]
  );
}

輸出:

array(3) {
  [0]=> 
  string(3) "4th"
  [1]=>
  string(4) "Alex"
  [2]=> 
  string(1) "A" 
}

Xpath表達式

  • 文檔中的任何student元素節點...
    //student
  • ...具有student子元素:
    //student[student]

Xpath表達式非常強大。 例如,您也可以獲取沒有student祖先節點的任何student

//student[not(ancestor::student)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM