简体   繁体   English

使用PHP解析InDesign中生成的XML

[英]Parse generated XML from InDesign with PHP

I'm generating XML from InDesign and would like to parse the XML in PHP. 我正在从InDesign中生成XML,并且想在PHP中解析XML。 Below is a sample of the XML that InDesign is generating: 以下是InDesign生成的XML的示例:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
<page title="About Us">
  About Us
  <page>Overiew</page>
  <page>Where We Started</page>
  <page>Help</page>
</page>
<page>
  Automobiles
  <page>
     Cars
     <page>Small</page>
     <page>Medium</page>
     <page>Large</page>
  </page>
  <page>
     Trucks
     <page>Flatbet</page>
     <page>
        Pickup
        <page>Dodge</page>
        <page>Nissan</page>
     </page>
  </page>
</page>
</Root>

I'm using the following PHP code to parse the XML recursively. 我正在使用以下PHP代码来递归解析XML。

header('Content-type: text/plain');

function parse_recursive(SimpleXMLElement $element, $level = 0)
{
        $indent     = str_repeat("\t", $level); // determine how much we'll indent

        $value      = trim((string) $element);  // get the value and trim any whitespace from the start and end
        $attributes = $element->attributes();   // get all attributes
        $children   = $element->children();     // get all children

        echo "{$indent}Parsing '{$element->getName()}'...".PHP_EOL;
        if(count($children) == 0 && !empty($value)) // only show value if there is any and if there aren't any children
        {
                echo "{$indent}Value: {$element}".PHP_EOL;
        }

        // only show attributes if there are any
        if(count($attributes) > 0)
        {
                echo $indent.'Has '.count($attributes).' attribute(s):'.PHP_EOL;
                foreach($attributes as $attribute)
                {
                        echo "{$indent}- {$attribute->getName()}: {$attribute}".PHP_EOL;
                }
        }

        // only show children if there are any
        if(count($children))
        {
                echo $indent.'Has '.count($children).' child(ren):'.PHP_EOL;
                foreach($children as $child)
                {
                        parse_recursive($child, $level+1); // recursion :)
                }
        }

        echo $indent.PHP_EOL; // just to make it "cleaner"
}

$xml = new SimpleXMLElement('data.xml', null, true);

parse_recursive($xml);

The issue that I'm having is that when I parse the XML, I'm not getting the text values of each page node unless completely surrounded by a page tag. 我遇到的问题是,当我解析XML时,除非完全被页面标签包围,否则我不会获得每个页面节点的文本值。 So, for example, I have no way of reading "About Us" unless looking at the title attribute (if it exists). 因此,例如,除非查看title属性(如果存在),否则我无法阅读“关于我们”。 The same applies for "Automobiles" and "Cars" and "Trucks". “汽车”,“汽车”和“卡车”也是如此。

Again, this is generated XML from InDesign. 同样,这是从InDesign生成的XML。 I could ask designers to add attributes to nodes, etc. but I'm trying to minimize the amount of data entry. 我可以要求设计人员将属性添加到节点等,但是我正在尝试最小化数据输入量。

I believe the XML is well formed. 我相信XML格式正确。 Any help would be greatly appreciated. 任何帮助将不胜感激。

You ignore all text values, if node has any childs, to change that replace: 如果节点具有任何子代,则忽略所有文本值以更改替换:

if(count($children) == 0 && !empty($value)) // only show value if there is any and if there aren't any children
{
  echo "{$indent}Value: {$element}".PHP_EOL;
}

with

if(!empty($value)) // only show value if there is anychildren
{
  echo "{$indent}Value: {$value}".PHP_EOL;
}

an then result with sample data is: 样本数据的结果是:

Parsing 'Root'...
Has 2 child(ren):
    Parsing 'page'...
    Value: About Us
    Has 1 attribute(s):
    - title: About Us
    Has 3 child(ren):
        Parsing 'page'...
        Value: Overiew

        Parsing 'page'...
        Value: Where We Started

        Parsing 'page'...
        Value: Help


    Parsing 'page'...
    Value: Automobiles
    Has 2 child(ren):
        Parsing 'page'...
        Value: Cars
        Has 3 child(ren):
            Parsing 'page'...
            Value: Small

            Parsing 'page'...
            Value: Medium

            Parsing 'page'...
            Value: Large


        Parsing 'page'...
        Value: Trucks
        Has 2 child(ren):
            Parsing 'page'...
            Value: Flatbet

            Parsing 'page'...
            Value: Pickup
            Has 2 child(ren):
                Parsing 'page'...
                Value: Dodge

                Parsing 'page'...
                Value: Nissan

Of course, I struggled with this but as soon as I ask the question I find the answer. 当然,我为此感到挣扎,但是当我提出问题时,我便找到了答案。 Anyway, this approach worked (top answer): 无论如何,这种方法有效(最佳答案):

How to get a specific node text using php DOM 如何使用php DOM获取特定的节点文本

I'm wondering if there's any other way, though. 我想知道是否还有其他方法。

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

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