2

I have an XML file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:element name="event">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="id" type="xsd:integer">55147</xsd:element>
            <xsd:element name="opponent" type="xsd:string">Mount Pleasant</xsd:element>
            <xsd:element name="game_date" type="xsd:string">2012-01-09</xsd:element>
            <xsd:element name="start_time" type="xsd:string">6:00pm</xsd:element>
            <xsd:element name="end_time" type="xsd:string"></xsd:element>
            <xsd:element name="sport" type="xsd:string">Basketball</xsd:element>
            <xsd:element name="level" type="xsd:string">Freshman</xsd:element>
            <xsd:element name="gender" type="xsd:string">Boys</xsd:element>
            <xsd:element name="year" type="xsd:string">2012</xsd:element>
            <xsd:element name="season" type="xsd:string">Winter</xsd:element>
            <xsd:element name="status" type="xsd:string"></xsd:element>
            <xsd:element name="homeaway" type="xsd:string">Away</xsd:element>
            <xsd:element name="facility"></xsd:element>
            <xsd:element name="facility_id" type="xsd:integer"></xsd:element>
            <xsd:element name="opponent">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="ss_id" type="xsd:integer">126</xsd:element>
                        <xsd:element name="name" type="xsd:string">Mount Pleasant</xsd:element>
                        <xsd:element name="score" type="xsd:string"></xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="location">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="ss_id" type="xsd:integer">1700</xsd:element>
                        <xsd:element name="name" type="xsd:string">Mt. Pleasant High School</xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="score">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="result" type="xsd:string"></xsd:element>
                        <xsd:element name="ours" type="xsd:string"></xsd:element>
                        <xsd:element name="theirs" type="xsd:string"></xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="league" type="xsd:string"></xsd:element>
            <xsd:element name="type" type="xsd:string">Game</xsd:element>
            <xsd:element name="ss_id" type="xsd:string">7504900</xsd:element>
            <xsd:element name="transportation" type="xsd:string"></xsd:element>
            <xsd:element name="dismissal" type="xsd:string"></xsd:element>
            <xsd:element name="return" type="xsd:string"></xsd:element>
            <xsd:element name="comment">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="school" type="xsd:string"></xsd:element>
                        <xsd:element name="conference" type="xsd:string"></xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

I tried:

var itemList = xml.documentElement.getElementsByTagName("event");
var game_date = itemList.getElementsByTagName("game_date").text;

in javascript to be able to parse this and get info from it. I would like to read from the tags here with data in them. I am having problems pulling the data out, as this looks like a schema with data. Is that supposed to happen?

1
  • This is not a valid XML schema. I would encourage whoever is creating this mess to get a clue. Commented Apr 3, 2012 at 15:47

2 Answers 2

2

As the XML structure is rather complex, I would use XPath to extract the information. To complicate matters slightly, you need a namespace resolver function to handle the xsd prefixes.

// Namespace resolver function
function nsResolver(prefix) {  
  var ns = {  
    'xsd' : 'http://www.w3.org/2001/XMLSchema'
  };  
  return ns[prefix] || null;  
} 

Extraction code

// Get a NodeList of all "event" elements
var eventNodeList = document.evaluate('/xsd:schema/xsd:element[@name="event"]', document, nsResolver, XPathResult.ANY_TYPE, null );  
// var eventNodeList = document.evaluate('/xsd:schema/xsd:element[@name="event"]', document);  

// Iterate over events
var currentEvent = eventNodeList.iterateNext();  
while (currentEvent) {  
  // Get value from "game_date" element
  var gameDate = document.evaluate('/xsd:complexType/xsd:sequence/xsd:element[@name="game_date"]/text()', currentEvent, nsResolver, XPathResult.ANY_TYPE,null).textContent

  // Do something with the value
  // ...

  // Get next event element
  currentEvent = eventNodeList.iterateNext();  
}
Sign up to request clarification or add additional context in comments.

Comments

0

The specified XML file is a XML schema (xsd). An XML schema is in itself an XML document, but it describes a structure, a contract, on the allowed elements, attributes and values of XML documents that claim to adhere to the schema.

If you examine the javascript code, you for example try to extract elements with the tag name "event", whereas the specified XML schema has elements called "xsd:element", "xsd:sequence" and so forth. What is slightly confusing is that the XML schema actually contains values, which is not common. Perhaps this is an attempt to exemplify values?

An XML instance, or document, of the above XML schema would look something like this

<event>
  <id>55147</id>
  <opponent>Mount Pleasant</opponent>
  ....
</event>

1 Comment

This XML file is a calendar getting pulled from a server. I have no control over it. All I need to do is parse it. Is there a way to read the values stored here? or what do I need to do?

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.