A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [RESOLVED] Linebreak (new line) between XML nodes

  1. #1
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971

    resolved [RESOLVED] Linebreak (new line) between XML nodes

    Hello, i don't achieve to add new lines (line breaks) between each XML node in a flash created xml object. I've read a lot of forums and i don't came up with a solution. I tried using "\n" as always, i tried appendChild, i tried "\n\r", i tried <![CDATA[<br/>]]>, i tried "<br/>", and i tried myXML.ignoreWhite = true;
    but when i print the data in the xml file, the nodes are not line-breaked.

    I need this:

    <First Node>
    <Property1>Value</Property1>
    <Property2>Value</Property2>
    <Property3>Value</Property3>
    </First Node>

    And i get:

    <First Node><Property1>Value</Property1<Property2>Value</Property2><Property3>Value</Property3></First Node>


    Hope someone can help
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    What is the code (*.fla) you are using to create your xml file, and by print do you mean print on paper on just viewing it

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Without seeing how you want it or how you are achieving it so far.

    This might be of assistance to you,
    PHP Code:
    var xml:XML = <FirstNode/>;

    var propertyAmount:Number = 5;

    for (var i:int = 1; i <= propertyAmount; i++)
    {
        var nodeName:String = "Property" + i;
        var isValue:String = "Value: " + i;
        xml.appendChild(<{nodeName}>{isValue}</{nodeName}>);
    }

    trace(xml);

    var saveXML:FileReference = new FileReference();
    saveXML.save("<?xml version='1.0' encoding='utf-8'?>\n\n" + xml, "savedXML.xml");

  4. #4
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Quote Originally Posted by fruitbeard View Post
    Hi,

    Without seeing how you want it or how you are achieving it so far.

    This might be of assistance to you,
    PHP Code:
    var xml:XML = <FirstNode/>;

    var propertyAmount:Number = 5;

    for (var i:int = 1; i <= propertyAmount; i++)
    {
        var nodeName:String = "Property" + i;
        var isValue:String = "Value: " + i;
        xml.appendChild(<{nodeName}>{isValue}</{nodeName}>);
    }

    trace(xml);

    var saveXML:FileReference = new FileReference();
    saveXML.save("<?xml version='1.0' encoding='utf-8'?>\n\n" + xml, "savedXML.xml");

    Yeah, that's exactly what i was doing, except this:

    saveXML.save("<?xml version='1.0' encoding='utf-8'?>\n\n" + xml,

    And i added that and i'm still getting all the nodes in the same line :/

    This is my script:
    PHP Code:
        switch(e.currentTarget)
        {
            case 
    b_save:
            var 
    temp:XML = new XML(); 
            
    temp.ignoreWhitetrue;
            
    temp = <Usuario/>;
            
    temp.appendChild(<Nombre>{t_name.text}</Nombre>);
            
    temp.appendChild(<Edad>{t_age.text}</Edad>);
            
    temp.appendChild(<Pais>{t_country.text}</Pais>);

            
    xml.appendChild(temp);
            
    trace(xml)
            
    fsave.save("\n"+xml"myXML.xml");
            break; 
    It works great, but in the XML file, all the nodes are put in the same line, and i need they to be
    aligned one in top of the other.

    I attach 2 pictures, one how it is supposed to looks like, and the other how it actually looks like, thanks so much for replying.

    Correct
    correct.JPG

    Wrong
    wrong.JPG
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  5. #5
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971

    Solved

    SOLVED. I achieved it. I don't know if it's the best practice, but it does what i wanted:

    Once the xml is constructed, we convert it to XML String this way:
    PHP Code:
    var newtext:String xml.toXMLString(); 
    Then, we replace all the "\n" with "\n\r" (this is the most important line of code, because this script will be sure that all the spaces will filled with the correct line breaks).
    PHP Code:
    newtext newtext.split('\n').join('\r\n'); 
    Finally, we make a new instance of the ByteArray class, and assign the string to it, so instead of writing the xml object into the XML file, we will write UTFBytes raw data:

    PHP Code:
    var bytes:ByteArray = new ByteArray();
    bytes.writeUTFBytes(newtext); 
    And then we write it:
    PHP Code:
    fsave.save(bytes"myXML.xml"); 
    Now the nodes are perfectly aligned in the XML file if you open it with Notepad.

    Picture:
    finally.JPG
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    or you could just use

    var newXML:String = oldXML.toXMLString();
    newXML = newXML.replace(/\n/g, "\r\n");

  7. #7
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Wow, that works great fruitbeard. No need of converting it to UTFBytes.

    I was about to tell you to explain that, but then i read about the replace class and learned that "g" means global, the first and the last "/" symbol are the opening and closing of the regular expresion, and the "\n" is the string to search. Thanks so much have a nice day.
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center