简体   繁体   English

获取XML标记和属性直至n级

[英]Get XML tag and attribute upto n level

Is it possible to get tag names and attributes names with values upto n levels. 是否可以获取标签名称和属性名称的值最多为n级。 For example, 例如,

File 1: 文件1:

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

File 2: 档案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>

File 3: 文件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>

Above are examples file 1 is simple file, file 2 is with more tags and file 3 is more longer than other. 上面是示例文件1是简单文件,文件2具有更多标签,文件3比其他文件更长。 So my basic question is that XML file format is not predefined it's may be anything just like above examples. 所以我的基本问题是XML文件格式不是预定义的,可能就像上面的示例一样。 So is it possible to get tags names, attribute names with its values in php? 那么是否有可能在PHP中获取标签名称,属性名称及其值? If yes then how? 如果是,那怎么办?

I already tried some solutions with $xml->children()->children() and $child->getName() i didn't find any perfect way normally it is good for 2 level or 3 level but no solution for N level xml tags. 我已经用$xml->children()->children()$child->getName()尝试了一些解决方案, $xml->children()->children()我找不到任何完美的方法,通常它适合2级或3级,但没有N级解决方案xml标记。

I tried as below : 我尝试如下:

$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'];
        }
    }
}

Note that file1, file2 & file3 example are different and above are different so don't confused i already mentioned that XML file & format are not predefined it may be anything any level. 请注意,file1,file2和file3的示例不同,并且上面的示例也不同,所以请不要混淆,我已经提到XML文件和格式不是预定义的,它可以是任何级别。

In above example i tried with 3 foreach loops but when it is upto N level then it's not working properly. 在上面的示例中,我尝试了3个foreach循环,但是当达到N级时,它无法正常工作。 I want to do totally dynamically. 我想完全动态地做。 Is it possible? 可能吗?

If you have more better ideas or ways then you can share. 如果您有更好的想法或方法,可以分享。 Thanks for giving your valuable time. 感谢您的宝贵时间。

I want to do following. 我想做以下。

I want to get some values like student name, fees, standard, stream, subjects, division and other normal details from XML. 我想从XML获取一些值,例如学生姓名,费用,标准,流,科目,部门和其他常规详细信息。 Now the main problem is XML format is not predefined it may be like as above examples that i mentioned. 现在的主要问题是XML格式未预定义,可能就像我提到的上述示例一样。 It is totally dynamic and i want to store in table as per the tags and/or attribute values. 它是完全动态的,我想根据标签和/或属性值存储在表中。

I hope are useful to understand what i want to say if any doubts then feel free to ask. 如果有任何疑问,可以随时询问,我希望能对我想说的有用。 Thanks. 谢谢。

Xpath expressions allow you to fetch nodes (and values) from the XML document. 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']
    ]
  );
}

Output: 输出:

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

The Xpath Expression Xpath表达式

  • any student element node in the document... 文档中的任何student元素节点...
    //student
  • ... that has a student child element: ...具有student子元素:
    //student[student]

Xpath expressions are quite powerful. Xpath表达式非常强大。 For example you could fetch any student that has no student ancestor node, too: 例如,您也可以获取没有student祖先节点的任何student

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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